Instanceof Vs getClass ()

I see a performance gain when using the getClass() and == operators over the instanceOf operator.

 Object str = new Integer("2000"); long starttime = System.nanoTime(); if(str instanceof String) { System.out.println("its string"); } else { if (str instanceof Integer) { System.out.println("its integer"); } } System.out.println((System.nanoTime()-starttime)); starttime = System.nanoTime(); if(str.getClass() == String.class) { System.out.println("its string in equals"); } else { if(str.getClass() == Integer.class) { System.out.println("its integer"); } } System.out.println((System.nanoTime()-starttime)); 

Is there any directive to use getClass() or instanceOf ?

Given the scenario: I know the exact classes that need to be mapped, i.e. String , Integer (these are final classes), etc.

Using the wrong practice of the instanceOf operator?

+75
java class instanceof
Feb 14 '11 at 7:40
source share
4 answers

The reason the performance of instanceof and getClass() == ... is different is because they do different things.

  • instanceof checks whether the object reference on the left (LHS) is an instance of the type on the right (RHS) or some subtype.

  • getClass() == ... checks if the types are identical.

Therefore, it is recommended that you ignore the performance issue and use an alternative that gives you the answer you need.

And yes, overuse of any of them is a "designer smell." If you are not careful, you will get a project in which adding new subclasses will lead to a significant alteration of the code. In most cases, the use of polymorphism is the preferred approach.

(There are exceptions. Classic in the implementation of equals(Object) , in which you need to check the type of the argument and return false if it does not match. Case, you usually need to use getClass() to properly execute the equals contract in the face of subclasses.)

+95
Feb 14 '11 at 7:44
source share

You want to exactly match the class, for example. just matching FileInputStream instead of any subclass of FileInputStream ? If so, use getClass() and == . I usually do this in equals , so an instance of X is not considered equal to an instance of a subclass of X, otherwise you may run into difficult problems with symmetry. On the other hand, it is more useful for comparing two objects of the same class than one particular class.

Otherwise use instanceof . Note that with getClass() you will need to make sure you have a non-zero reference to start with, or you will get a NullPointerException , while instanceof will just return false if the first operand is null.

Personally, I would say that instanceof more idiomatic, but in most cases using any of them as a whole is a design odor.

+28
Feb 14 2018-11-11T00:
source share

I know that some time has passed since this was set, but yesterday I found out an alternative

We all know what you can do:

 if(o instanceof String) { // etc 

but what if you donโ€™t know exactly what type of class it should be? you cannot do in general:

 if(o instanceof <Class variable>.getClass()) { 

as it gives a compilation error.
Instead, here is an alternative - isAssignableFrom ()

For example:

 public static boolean isASubClass(Class classTypeWeWant, Object objectWeHave) { return classTypeWeWant.isAssignableFrom(objectWeHave.getClass()) } 
+13
Sep 26 '12 at 20:55
source share

getClass () has a restriction that objects are equal only to other objects of the same class, to the same type of runtime, as shown in the output below the code:

 class ParentClass{ } public class SubClass extends ParentClass{ public static void main(String []args){ ParentClass parentClassInstance = new ParentClass(); SubClass subClassInstance = new SubClass(); if(subClassInstance instanceof ParentClass){ System.out.println("SubClass extends ParentClass. subClassInstance is instanceof ParentClass"); } if(subClassInstance.getClass() != parentClassInstance.getClass()){ System.out.println("Different getClass() return results with subClassInstance and parentClassInstance "); } } } 

Outputs:

SubClass extends ParentClass. subClassInstance - an instance of the ParentClass class.

Various getClass () return results using subClassInstance and parentClassInstance.

+2
May 17 '16 at 17:16
source share



All Articles