I was wondering how computationally expensive is using instanceof operator in java and wanted to know if there are any better alternatives available
4 Answers
The alternative is to avoid using instanceof and design your classes properly (in OO sense).
As the instanceof operator has a corresponding "instanceof" byte code instruction, there probably won't be a more performant approach; but this may also depend on how the actual JVM optimizes.
4 Comments
instanceof is often a related to bad design. For me, not using it is the best alternative. And if you are not using it, you don't need to be concerned about it's performance.instanceof is pretty damn fast. However, it's generally a symptom of a poorly thought out design.
It'll have around the same performance as a (successful) cast, as it is doing much the same thing. Indeed, the task is roughly equivalent to a "virtual" method call.
On sane implementations: For classes, it's just a matter of getting the runtime class and looking at a fixed offset to check the superclass (so long as you don't have an inheritance chain of more than eight classes for HotSpot). Interfaces are a bit more tricky, but generally have the last two used cases for any particular runtime class cached. So that's also fast.
Comments
I assume you have actually profiled your code and found that your use of instanceof is a non-trivial performance hit? If not, you're almost certainly solving a problem that isn't worth your time to be solving.
If all you're doing is code like this:
if ( something instanceof MyClass ) {
MyClass mySomething = (MyClass) something;
//...
} else {
//exceptional case
}
Then it might be possible to try the cast first, and allow the ClassCastException to be your "exceptional case":
try {
MyClass mySomething = (MyClass) something;
} catch (ClassCastException cce) {
//exceptional case
}
Now, while it may be premature optimization, it would not be premature to rethink your design. Overuse of instanceof is a design smell. In general, you should be using generics and polymorphism in ways that reduce the number of times you'd use instanceof (and indeed casting) to (nearly) zero.
If different code should be run depending on the type of the object, consider making that code an instance method of the object and having the different types conform to an interface.
If you find yourself "knowing" that an object is of a certain type but you've done some step that makes the compiler lose track of that fact (e.g. you put it into a raw
List), it might be a candidate for generification.
Comments
If you want to check if the object is an instance of a certain class (but not if it extends or implements it), maybe comparing the classes with == would be faster :
o.getClass() == YourClass.class
Otherwise since the instanceof keyword has been created for this specific purpose I don't see how you could ever do better..
2 Comments
instanceof bytecode operation. Your approach adds the method call getClass.getClass will not work when attempting to check whether the object also belongs to a certain subclass. For instance, if you have FileReader extending InputStreamReader which in turn extends/implements Reader, calling getClass on an object instantiated with new FileReader will never yield anything else than FileReader class, and so o.getClass() == Reader or o.getClass() == InputStreamReader will both evaluate to false. instanceof, on the other hand, will yield true in all three class cases.