instanceof operator in java

In Java, instanceof operator is used to check the type of an object at runtime.Basically instanceof operator is helpful for finding the type of object at run time. In other words , instanceof operator is used to check whether the object is an instance of the specified type (class or subclass or interface).

  instanceof operator return boolean value, if an object reference is of specified type then it return true otherwise false.


Example of instanceof operator

Let us see simple of example of instanceof operator.

class Test{
 public static void main(String[] args) {
 	Test t=new Test();
 	boolean b=t instanceof Test;
 	System.out.println(b);//true
 }
}
true

Note :An object of child type is also a type of parent class. For example, if Dog extends Animal then object of Dog can be referred by either Dog or Animal class.

Another example of java instanceof operator

class Animal{}
class Dog1 extends Animal{//Dog inherits Animal

 public static void main(String args[]){
 Dog1 d=new Dog1();
 System.out.println(d instanceof Animal);//true
 }
}
true

instanceof in java with a variable that have null value

If we apply instanceof operator with a variable that have null value, it returns false. Let's see the example given below where we apply instanceof operator with the variable that have null value.

class Example{
 public static void main(String args[]){
  Example e=null;
  System.out.println(e instanceof Example);//false
 }
}
false

Downcasting with java instanceof Operator

When Subclass type refers to the object of Parent class, it is known as downcasting. If we perform it directly, compiler gives Compilation error. If you perform it by typecasting, ClassCastException is thrown at runtime. But if we use instanceof operator, downcasting is possible.

  Example of down casting

down casting

Child d=new Parent();//Compilation error

If we perform downcasting by typecasting, ClassCastException is thrown at runtime.

Child d=(Child )new Parent();//Runtime error
Compiles
 successfully but ClassCastException is thrown at runtime.

Example of downcasting with instanceof

class Animal { }  
  
class Dog3 extends Animal {  
  static void method(Animal a) {  
    if(a instanceof Dog3){  
       Dog3 d=(Dog3)a;//downcasting  
       System.out.println("ok downcasting performed");  
    }  
  }  
   
  public static void main (String [] args) {  
    Animal a=new Dog3();  
    Dog3.method(a);  
  }  
 }  
ok downcasting performed

we typecast object after checking the object is of type child or not.

Another example of downcasting with instanceof

class Parent{ }

public class Child extends Parent
{
    public void check()
    {
        System.out.println("Sucessfull Casting");
    }

    public static void show(Parent p)
    {
       if(p instanceof Child)
       {
           Child b1=(Child)p;
           b1.check();
       }
    }

    public static void main(String[] args)
    {

      Parent p=new Child();

      Child.show(p);
      }
}
Sucessfull Casting