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
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!

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

WannaCry ransom notice analysis suggests Chinese linkPosted on Monday May 29, 2017

Researchers say the WannaCry ransom note was poorly translated - possibly using Google Translate.

BA boss 'won't resign' over flight chaosPosted on Monday May 29, 2017

Chief executive Alex Cruz says flight disruption at Heathrow and Gatwick had nothing to do with cost cutting.

Hay Festival 2017: Stephen Fry's warning for the webPosted on Sunday May 28, 2017

Delivering a lecture at the Hay Festival, the actor said society could face "dire consequences".

Courtesy BBC News