1

What is better to check if an object is an instance of a class?

object instanceof class

or

object == class.instance

Is it better to use first option where I need to import the class into another class to check if the object is an instance of the class, or to check if its equal to an object of the class initialized in its class?

I'm currently using instanceof method but I'm not sure if it's better to use that or maybe some other solutions.

marcospereira
  • 12,045
  • 3
  • 46
  • 52
Warix3
  • 99
  • 1
  • 11
  • You don't actually mean to write string literals, right? – Andy Turner Feb 29 '16 at 21:06
  • I don't understand the 2nd example. Did you mean to write something like `object.getClass() == SomeClass.class`? – radoh Feb 29 '16 at 21:07
  • 2
    It's unclear what you actually mean by the second case. If you want to check if an object is an *instance of* a class, `object instanceof AClass` is obviously the way to do it. – Andy Turner Feb 29 '16 at 21:08
  • Second case is checking if the object is equal to an object who is an instance of the class. – Warix3 Feb 29 '16 at 21:10
  • The second case is checking if an object is *identical* to an object which is an instance of a class, not equal - [`==` is not `equals`](http://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and). – Andy Turner Feb 29 '16 at 21:11
  • So if i have an indentical object, is it better to check if the first one is identical to second one, or to check if its an instance of a class. If i just want to confirm it's an instance of the class. – Warix3 Feb 29 '16 at 21:15
  • What do you think? Bonus question: do you think the difference matters on any practical level? Bonus question 2: how about measuring it? – biziclop Feb 29 '16 at 21:17
  • Just to clarify, by "the difference", I mean the speed difference, not the fact that the two options may not even be functionally equivalent. – biziclop Feb 29 '16 at 21:22
  • How do i measure that? – Warix3 Feb 29 '16 at 21:25
  • @ToniPejic Very good question. [Here's](http://openjdk.java.net/projects/code-tools/jmh/) a little intro and a fun tool to tinker with. It really is fun to test hypotheses like this, but we should always remember that even with the most carefully written test we may not be measuring what we think we are. – biziclop Feb 29 '16 at 21:39
  • But what you'll find in this case is that the speed difference is so small that it really isn't worth worrying about. And of course your result will only be valid for that particular version of that particular VM on that particular machine. – biziclop Feb 29 '16 at 21:51

2 Answers2

3

obj instanceof MyClass is generally the preferred way, unless it must be a MyClass and not a subclass of MyClass, in which case obj.getClass() == MyClass.class is the check to use.

obj == class.instance makes no sense.
obj == new MyClass() would always return false, and makes no sense either.
obj == otherObj is an identity check of object instances, and is meaningless for checking if they are the same class, because unequal objects could still be of same class.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thank you for explainig, by "obj == class.instance" i meant class.instance to be an object who is an instance of that class (an object of that class already defined in that class) – Warix3 Feb 29 '16 at 21:22
0

Two options are not same. let me explain

first one is checking if the given instance 'object' is of type 'class' (or a subtype of 'class') . e.g

class A {}

class B extends A {}

Object b = new B();

if( b instanceof A ){
 //will be true
} 

Second option is not even valid.

using 'instanceof' is preferred way to find out if an object is instance of particular type.

Peeyush
  • 422
  • 3
  • 13