Instanceof Operator in Java

The instanceof operator in java is used to test whether the object is an instance of the specified type (class or subclass or interface). The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

Lets see a simple program example –

Run Online

// Java program to demonstrate working of instanceof
 
// Creating sample classes with parent Child
// relationship
class Parent {  }
class Child extends Parent { }
 
class Test
{
    public static void main(String[] args)
    {
        Child cobj = new Child();
 
        // A simple case
        if (cobj instanceof Child)
           System.out.println("cobj is instance of Child");
        else
           System.out.println("cobj is NOT instance of Child");
 
        // instanceof returns true for Parent class also 
        if (cobj instanceof Parent)
           System.out.println("cobj is instance of Parent");
        else
           System.out.println("cobj is NOT instance of Parent");
   
    }
}
Output
cobj is instance of Child
cobj is instance of Parent
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 Dog2
{  
   public static void main(String args[])
   {  
      Dog2 d=null;  
      System.out.println(d instanceof Dog2);//false  
   }  
}
Output
false
Downcasting with java instanceof operator

upcasting vs downcasting in java

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.

Lets see a simple program example –

Run Online

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);  
  }  
 }
Output
ok downcasting performed

4 thoughts on “Instanceof Operator in Java

  • May 14, 2018 at 1:55 pm
    Permalink

    Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform. I would be awesome if you could point me in the direction of a good platform.

    Reply
    • May 14, 2018 at 2:52 pm
      Permalink

      Well yes this is definitely an off topic question on this article. I am not really aware of other platforms, maybe drupal or joomla. These are the only ones I just have heard of. All the best.

      Reply
  • May 26, 2018 at 7:35 pm
    Permalink

    I really can’t believe how great this site is. Keep up the good work. I’m going to tell all my friends about this place.

    Reply
    • May 27, 2018 at 1:29 pm
      Permalink

      Thank you very much 🙂 Yes please do share it with your friends, that would be the best help for our website 🙂

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *