2 Replies Latest reply: Nov 3, 2006 1:52 AM by 3004 RSS

    instanceOf vs Class.forName("...").isInstance(obj)

    807598
      Can anyone please tell me what's the difference between

      instanceOf Operator and Class.forName("...").isInstance(obj) metod?

      Basically I want to know which is more efficient?

      Thanks
        • 1. Re: instanceOf vs Class.forName("...").isInstance(obj)
          791266
          [edit]

          The javadoc:

          Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.


          Kaj

          Message was edited by:
          kajbj
          • 2. Re: instanceOf vs Class.forName("...").isInstance(obj)
            3004
            instanceof requires you to know the class you want to compare against at compile time. isInstance does not.

            instanceof will probably be slightly faster, but you probably won't notice any difference in most situations.