What is the difference between instanceof operator and isInstance() method in Java?

Both isInstance() method and instanceof operator (type comparison operator) are used to check if an object is of a particular class or interface type, yet there are differences. Let's see those differences between isInstance() and instanceof. The right-hand-operand of instanceof operator must be evaluated to a type (class or interface), which is known at compile time. There may be chances when you don't know the type name at compile time and you want to pass it as an argument that will be resolved at run time. In those circumstances you won't be able to use instanceof operator because instanceof does not work for types evaluated at run time.

The isInstance() method, which is dynamic equivalent of the Java language instanceof operator does type checking at run time. The public boolean isInstance(Object obj) method 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 casted to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

The term dynamic equivalent is used because isInstance() method is used to check type of a reference at run time. The very important application use of isInstance() method is it can be used in code dealing with type reflection at runtime. Following is a small Java program demonstrates isInstance() and instanceof.

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
    System.out.println("a.getClass().isInstance(a): " + a.getClass().isInstance(a)); //true
    System.out.println("b.getClass().isInstance(c): " + b.getClass().isInstance(c)); //false
  }
}
 
OUTPUT
======
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
a.getClass().isInstance(a): true
b.getClass().isInstance(c): false

In above piece of code, you might have noticed that expression a instanceof B returns false. This is because, at run time, the result of the instanceof operator is true if the value of the left-hand operand is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

Hope you have enjoyed reading difference between isInstance method and instanceof operator in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

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