Quantcast
Channel: Scratch Where It's Itching
Viewing all articles
Browse latest Browse all 73

Instanceofities

$
0
0
If I am not mistaken, the following three lines of code do the exact same thing:
System.out.println( myObj instanceof MyClass);
System.out.println( MyClass.class.isInstance(myObj));
System.out.println( MyClass.class.isAssignableFrom(myObj.getClass()));

The first line is to be used if you know the Class literal at compile time. You use the second line when you do not have the type information at compile time, and the third one if the you have no instance of the object to be tested at your disposal. But still, I see people using the second or even third line when they could simply use the first one. Is there any known reason for that?


Viewing all articles
Browse latest Browse all 73

Trending Articles