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

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

      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?

        • 1. Re: instanceOf vs Class.forName("...").isInstance(obj)

          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.


          Message was edited by:
          • 2. Re: instanceOf vs Class.forName("...").isInstance(obj)
            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.