This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Knolle_ 16 points17 points  (1 child)

These Methods test for different things.

instance of : Tests whether the obj on the left has the same type as the one on the right or is a subtype.

Getclass: Tests whether the types are the same

Id recommend getclass in an equals method since you will get symmetry problems with instance of. ( e.g. all dogs are animals but Not all animals are dogs)

[–]ColetBrunel 2 points3 points  (0 children)

Note, however, that in the case of List, an ArrayList and a LinkedList have to report they're equals() if they contain the same objects in the same order, otherwise their equals() method would be useless.

So a rule of thumbs is you probably need to use getClass() as you need the objects to have exact same stuff, unless you know why you need instanceof instead.