Java Tutorial Java Advanced Java References

Java instanceof Keyword



The Java instanceof keyword is an operator and it is used to check if an object is an instance of a specific type (class, subclass or interface). It returns true if the object is an instance of a specific type, false otherwise.

Syntax

objectName instanceof objectType

Return Value

Returns true if the object is an instance of a specific type (class, subclass or interface), false otherwise.

Example:

The example below shows the usage of instanceof operator.

public class MyClass {
  public static void main(String[] args) {
    String str = "Hello World";
    MyClass obj = new MyClass();

    //using instanceof operator
    boolean x = str instanceof String;
    System.out.println("str instanceof String: " + x); 

    //using instanceof operator
    boolean y = obj instanceof MyClass;
    System.out.println("obj instanceof MyClass: " + y); 
  }
}

The output of the above code will be:

str instanceof String: true
obj instanceof MyClass: true

Java instanceof during Inheritance

The instanceof operator can be used to check whether an object of a subclass type is also an instance of parent class. Consider the example below:

class Shape{}
class Circle extends Shape {}

public class MyClass {
  public static void main(String[] args) {
    Shape sobj = new Shape();
    Circle cobj = new Circle();

    //using instanceof operator
    if(sobj instanceof Shape) 
      System.out.println("sobj is an instance of Shape.");
    else
      System.out.println("sobj is NOT an instance of Shape."); 

    if(sobj instanceof Circle) 
      System.out.println("sobj is an instance of Circle.");
    else
      System.out.println("sobj is NOT an instance of Circle."); 

    if(cobj instanceof Shape) 
      System.out.println("cobj is an instance of Shape.");
    else
      System.out.println("cobj is NOT an instance of Shape."); 

    if(cobj instanceof Circle) 
      System.out.println("cobj is an instance of Circle.");
    else
      System.out.println("cobj is NOT an instance of Circle.");       
  }
}

The output of the above code will be:

sobj is an instance of Shape.
sobj is NOT an instance of Circle.
cobj is an instance of Shape.
cobj is an instance of Circle.

Java instanceof in Interface

The instanceof operator can be used to check whether an object of a class type is also an instance of the interface implemented by the class. Consider the example below:

interface Shape{}
class Circle implements Shape {}

public class MyClass {
  public static void main(String[] args) {

    Circle cobj = new Circle();

    //using instanceof operator
    if(cobj instanceof Shape) 
      System.out.println("cobj is an instance of Shape.");
    else
      System.out.println("cobj is NOT an instance of Shape."); 

    if(cobj instanceof Circle) 
      System.out.println("cobj is an instance of Circle.");
    else
      System.out.println("cobj is NOT an instance of Circle.");       
  }
}

The output of the above code will be:

cobj is an instance of Shape.
cobj is an instance of Circle.

❮ Java Keywords