Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instanceof vs equals on defined string that identifies the Class

Tags:

java

I can test the class type in a program where I'm working both with instanceof or getting a field that identifies the class appartenency. so the choice is between

if(myObjec instanceof Something){
} 

And

if(myObjec.geClassType().equals("Something")){
}

What is the best in terms of efficency?

geClassType() is a method present in the object of the program where I'm working(not a standard java method) and is a method that returns a string wih the name of the class. the name is passed as field when the object is created

EDIT the field that I get in the second sample in my code doesn't identify exactly the class but a category and is used also for different routines, but I can use this also for this purpose since I use a specific object for each category. So I have reported this as getClassType to make my question more clear.

like image 261
AndreaF Avatar asked Mar 20 '23 19:03

AndreaF


2 Answers

instanceof is more efficient, actually works (since geClassType() doesn't exist) and is much more readable.

If instanceof is the behaviour that you want (as other answers point out the behaviour is potentially different between the two methods you outline) I'm not sure why you'd even consider the alternative?

EDIT: I should reiterate, even if you have a method in your context that does do what you describe, I still can't see a good reason to do it that way. It may introduce bugs if it doesn't work exactly the way you expect all the time (such as with similarly named classes in different packages), it will likely be slower, but more importantly it's non standard so makes your code much less readable.

In short, instanceof is a useful keyword that does exactly what you want, reliably and readably, with the minimum of fuss. Why on earth wouldn't you use it?!

like image 65
Michael Berry Avatar answered Apr 06 '23 04:04

Michael Berry


You probably mean what's the difference if verifying if the object reference belongs to a specific class using instanceof vs comparing the name of the class.

instanceof works for object references that are from the class or for a subclass of the class being compared. In case of interfaces, it returns true if the class (or a super class) of the object reference implements this interface.

getClass().getName().equals("NameOfTheClass") (masked by your getClassType method) will verify if the class of the object reference is exactly from another class, thus a sub class won't pass this test.

Which one to use? Use the one that better suits for your case:

  • If you want/need to use object references that belongs to a certain class or interface, despite if the class of the object reference is a subclass or implements the interface, then use instanceof. For example, when iterating through the elements of a Collection to verify if some objects implement a desired interface.

  • If you need to determine if the class of the object reference is exactly a specific class, use getClass().getName().equals("..."). For example, when implementing equals method in a class that should compare to elements that belongs only to this class (no subclasses allowed). Some IDEs do this when auto generating equals method.

Still, a better option than getClass().getName().equals("...") would be getClass().equals(TheClass.class).


In terms of efficiency, if you need to verify if the object belongs to a specific class only, use getClass().equals(TheClass.class) rather than getClass().getName().equals("...") because one will compare references (which is as fast as instanceof). But if you only have the class name, use your current approach. Do not worry about the performance of the application because the JIT will optimize the code at runtime for you after several executions of the code. Still, f you think this statement may be a performance bottleneck, prove it using a profiler.

like image 45
Luiggi Mendoza Avatar answered Apr 06 '23 03:04

Luiggi Mendoza