Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

instanceof operator

The java instanceof operator 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.

Simple example of java instanceof

Let’s see the simple example of instance operator where it tests the current class.

  1. class Simple1{
  2.  public static void main(String args[]){
  3.  Simple1 s=new Simple1();
  4.  System.out.println(s instanceof Simple1);//true
  5.  }
  6. }

An object of subclass 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

  1. class Animal{}
  2. class Dog1 extends Animal{//Dog inherits Animal
  3.  public static void main(String args[]){
  4.  Dog1 d=new Dog1();
  5.  System.out.println(d instanceof Animal);//true
  6.  }
  7. }

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.

  1. class Dog2{
  2.  public static void main(String args[]){
  3.   Dog2 d=null;
  4.   System.out.println(d instanceof Dog2);//false
  5.  }
  6. }

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.

  1. Dog d=new Animal();//Compilation error

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

  1. Dog d=(Dog)new Animal();
  2. //Compiles successfully but ClassCastException is thrown at runtime

Possibility of downcasting with instanceof

Let’s see the example, where downcasting is possible by instanceof operator.

  1. class Animal { }
  2. class Dog3 extends Animal {
  3.   static void method(Animal a) {
  4.     if(a instanceof Dog3){
  5.        Dog3 d=(Dog3)a;//downcasting
  6.        System.out.println(“ok downcasting performed”);
  7.     }
  8.   }
  9.   public static void main (String [] args) {
  10.     Animal a=new Dog3();
  11.     Dog3.method(a);
  12.   }
  13.  }
Output:ok downcasting performed

Downcasting without the use of java instanceof

Downcasting can also be performed without the use of instanceof operator as displayed in the following example:

  1. class Animal { }
  2. class Dog4 extends Animal {
  3.   static void method(Animal a) {
  4.        Dog4 d=(Dog4)a;//downcasting
  5.        System.out.println(“ok downcasting performed”);
  6.   }
  7.    public static void main (String [] args) {
  8.     Animal a=new Dog4();
  9.     Dog4.method(a);
  10.   }
  11. }
Output:ok downcasting performed

Let’s take closer look at this, actual object that is referred by a, is an object of Dog class. So if we downcast it, it is fine. But what will happen if we write:

  1. Animal a=new Animal();
  2. Dog.method(a);
  3. //Now ClassCastException but not in case of instanceof operator

Understanding Real use of instanceof in java

Let’s see the real use of instanceof keyword by the example given below.

  1. interface Printable{}
  2. class A implements Printable{
  3. public void a(){System.out.println(“a method”);}
  4. }
  5. class B implements Printable{
  6. public void b(){System.out.println(“b method”);}
  7. }
  8. class Call{
  9. void invoke(Printable p){//upcasting
  10. if(p instanceof A){
  11. A a=(A)p;//Downcasting 
  12. a.a();
  13. }
  14. if(p instanceof B){
  15. B b=(B)p;//Downcasting 
  16. b.b();
  17. }
  18. }
  19. }//end of Call class
  20. class Test4{
  21. public static void main(String args[]){
  22. Printable p=new B();
  23. Call c=new Call();
  24. c.invoke(p);
  25. }
  26. }
Output: b method