Differences between "foo instanceof Type" and "foo.getClass () == Type.class / fuu.getClass ()"

1

When determining if an object belongs to a type, basically one of these two solutions is used:

foo instanceof Tipo;

O

foo.getClass() == Tipo.class;

The question is: What advantages and disadvantages does each one offer and in which cases is it better to use them?

  

NOTE: Even if I put an answer I would like to know if it is complete, correct, it can be improved or there is something that has happened to me.

    
asked by Awes0meM4n 30.11.2016 в 13:06
source

1 answer

2

instanceof

According to the java documentation :

  

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

That is, instanceof will return true if the object on the left is an instance of the type specified on the right, a subtype or implements its interface.

.getClass () ==

Use the operator == equal to to compare the two parts returning only true if they are exactly the same. This is that the two parts have to be exactly of the same class.

Differences and usage

From the above we have that instanceof serves to know if an object belongs to a class or "his daughters". This includes the anonymous classes and makes it very useful to maintain an elegant and concise code where a simple change by overwriting a method would otherwise give us an erroneous comparison. The downside is that we can not compare one object against another, but we must specify a type to its right .

On the other hand, class == class allows you to compare one object against another or against a class and if you return true we know that both parties will behave the same because they are exactly the same type. p>

Also the operator == is transitive (it can be read from left to right and vice versa with the same result) while instanceof is not evaluate differently if we "change" the sense of evaluation.

This is especially important to take into account when implementing methods such as equals , hashCode or compareTo since they need to be transitive or we would have a contract error if in execution it will be detected true that (A > B & B > C & A <= C) or (A.equals(B) != B.equals(A)) . Therefore in implementations of transitive methods you should NOT use instanceof .

These are the cases that have occurred to me to see the difference with their output:

  

Operation class == class
  subtype vs SubClase: (subtype.getClass () == Hija.class)) is true
  subtype vs anonymous: (subtype.getClass () == anonimo.getClass ()) is false
  anonymous vs subClass: (anonimo.getClass () == TablaNiveles.class) is false
  anonymous vs SuperClass: (anonymous.getClass () == JTable.class) DOES NOT COMPLETE

Operation Object instanceof Class
  subtype vs anonymous: (subtype instanceof anonimo.getClass ()) DO NOT COMPILATE
  subtype vs SubClass: (subtype instanceof Daughter) is true
  subtype vs SuperClase: (subtype instanceof Father) is true
  anonymous vs SubClase: (anonymous instanceof Daughter) is true
  anonymous vs SuperClase: (anonymous instanceof Father) is true

    
answered by 30.11.2016 / 13:06
source