What is the use of instanceof or type comparison operator in Java?

The instanceof operator also called type comparison operator is used to check if an object is of a particular class or interface type. The instanceof operator is used for object references only to test the class of an object. The instanceof operator works on the principle of IS~A test. In Object Oriented Programming, the concept of IS-A is based on class inheritance or interface implementation. IS-A is a way of saying, "this object is a type of that class." We express the IS-A relationship in Java through the keywords extends (for class inheritance) and implements (for interface implementation).

The instanceof operator has the following syntax:

object instanceof type

The instanceof operator evaluates to true if the expression on its left is a reference type that is assignment compatible with the type name on its right, and false otherwise. Term "assignment compatible" means that we can test an object reference against its own class type, or any of its superclasses. Hence, any object reference will evaluate to true if we use the instanceof operator against type Object because in Java every class implicitly inherits Object class. It is important to note that null is not an instance of any type; therefore, instanceof for null always returns false.

Important points about instanceof operator:

  • An indirect implementation occurs when one of an object's superclasses implements an interface, but the actual class of the instance does not. An object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the object's superclasses implement the interface.
  • You cannot use instanceof to see if an object is an instance of a parameterized type, again with the exception of a parameterized type whose type arguments are all unbounded wildcards. The instanceof test is a runtime test, and at runtime the parameterized type has been replaced by its erasure. You can replace the parameterized type by its erasure if that is what you really wanted to test.
  • Note that arrays are objects, even if the array of primitives; therefore, an array is always an instance of Object.
  • There will be a compile time error if instanceof operator is evaluated on two different class hierarchies. For example, the following piece of code won't compile because classes A and D are two altogether separate classes and have no IS~A relationship.
class A {}
class D { }
public class InstanceofDemo
  public static void main(String args[])
    A a = new A();
    D d = new D();
    //compile time error: inconvertible types
    System.out.println("d instanceof A: " + (d instanceof A));

Application Use of instanceof operator:

  • It is good programming practice to use instanceof operator to find out whether a cast will succeed before attempting it.

Following is a small Java program demonstrating instanceof operator:

class A {}
class B extends A {}
class C extends A {}
public class InstanceofDemo
  public static void main(String args[])
    A a = new A();
    B b = new B();
    C c = new C();
    System.out.println("a instanceof A: " + (a instanceof A)); //true
    System.out.println("b instanceof A: " + (b instanceof A)); //true
    System.out.println("c instanceof A: " + (c instanceof A)); //true
    System.out.println("a instanceof B: " + (a instanceof B)); //false
    System.out.println("null instanceof A: " + (null instanceof A)); //false
D:\JavaPrograms>javac InstanceofDemo.java
D:\JavaPrograms>java InstanceofDemo
a instanceof A: true
b instanceof A: true
c instanceof A: true
a instanceof B: false
null instanceof A: false

The isInstance() method: dynamic equivalent of instanceof

There is a method also, which is dynamic equivalent of the Java language instanceof operator. In java.lang there is a class Class has a public method isInstance that determines if the specified Object is assignment-compatible with the object represented by this Class. This method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

Hope you have enjoyed reading the use of instanceof operator. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

AlphaBay and Hansa dark web markets shut downPosted on Thursday July 20, 2017

The AlphaBay and Hansa marketplaces were known for trade in drugs, weapons and malware.

Kodi magazine 'directs readers to pirate content'Posted on Thursday July 20, 2017

The guide is being investigated by the Federation Against Copyright Theft.

Bitcoin swings as civil war loomsPosted on Thursday July 20, 2017

The Bitcoin community must agree how to tackle a slowdown in transaction times to avoid a schism.

Courtesy BBC News


Advertisements help running this site for free.

To view the content please disable AdBlocker and refresh the page.