instanceof : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

instanceof different objects instances of one class instanceof
instanceof is a curious beast. It is an operator, not a function. It has more powers than many give it credit for and it lacks some you might expect of it. I summarise with this table:
Code Effect
dog instanceof Dog true
dalmatian instanceof Dog true
dog instanceof Dalmatian false
dalmatian instanceof ShowDogInterface true
dalmatian instanceof dog syntax error
! dalmatian instanceof Dog syntax error
! (dalmatian instanceof Dog) false
dog instanceOf Dalmatian syntax error
dalmatian instanceof Dog syntax error
dalmatian instanceof Class.forName (DogPackage.Dog ) syntax error
null instanceof String false

Because of laziness on the part of compiler writers, instanceof cannot deal with a class determined dynamically using Class.forName. For that, you must say something like:
if ( Class.forName ( classNameString ).isInstance ( myObject ) ) ...
Just to keep you on your toes, there are yet other methods with similar names, java.beans.Beans.isInstanceOf and java.beans.Beans.getInstanceOf.

Implementation

If you are like me, you are curious about what goes on inside the JVM (Java Virtual Machine). How does it implement instanceof? instanceof logic gets used quite frequently, every time you explicitly use instanceof, every time you cast, every time you store an object into an array. How could instanceof be implemented to make this operation quick?

It turns out that everything depends on the type of the desired instance and the actual type of the object. The type of the object’s reference is irrelevant, though obviously it could be used to optimise away silly instanceofs such as Dog instanceof Dog.

One technique is to assign class numbers by walking the inheritance tree of Object depth first. Then all descendants of a given object fall in a range m to n and no other classes fall in that range. The problem comes when a new class is loaded, especially via Class.forName which would upset the numbering scheme. Perhaps it could leave holes for new classes to fit in. Only rarely would a complete renumbering be needed.

Another brute force technique is to compare the test object’s class with the desired class. If that fails, compare the superclass, etc, right on back to Object.

Here is yet another technique: I want to know if a given Dog reference is a Dalmatian. We know that class nesting depth works like this:

depth class name
3 MostlyBlackDalmatian
2 Dalmatian
1 Dog
0 Object
So all you have to test is that the class of your reference has at least depth 2 and at depth 2 in its class hierarchy is Dalmatian. This works even if new classes are added. This means your object needs a pointer to description of the class hierarchy. The classes could be assigned numbers (or addresses) (in load order) in addition to names, for comparing speed.

For interfaces, it is more complex since classes can implement multiple interfaces. Interfaces can inherit from other interfaces and classes inherit the interfaces implemented by their ancestors. This is one reason why you attempt to use class references rather than inferface references in time critical code. There you need a bit map indexed by interface number for each class to say whether it implements the interface.

One trick is to cache the class of the last object for which this particular instanceof was called. Chances are good that the next time the Object in question will have the same class. This works for both classes and interfaces.

Speed

Initially, Java used a rather flat-footed implementation of instanceof. Further, you also paid quite a penalty for invoking a method via an interface reference rather than a class reference. Over the years, the implementation has become cleverer, first through caching and later reputedly through even cleverer techniques, though I have not seen an essay exactly on how it now works. Further, the Jet optimising compiler, using static compile-time analysis can narrow down the possible classes for an object, sometimes down to one, thus bypassing the need for any run time work.

Learning More

Oracle’s Javadoc on Class.isInstance : available:
Oracle’s Javadoc on Class.isAssignableFrom : available:
Oracle’s Javadoc on Object.getClass : available:
Oracle’s Javadoc on Class.getSuperclass : available:
Oracle’s Javadoc on Class.asSubclass : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/instanceof.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\instanceof.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[44.192.67.10]
You are visitor number