«Back to Home

Core Java

Topics

Instanceof Operator In Java

Instanceof Operator
 
Java instanceof operator is used to compare the object is an instance of the specific class or subclass or interface. It compares the instance with type due to which it is also called as a comparison operator. It returns either true or false only.
 
Let’s see an example, given below.
 
Code
  1. public class Person {  
  2.     public static void main(String args[]) {  
  3.         Person p = new Person();  
  4.         System.out.println(p instanceof Person);  
  5.     }  
  6. }  
5
 
Output

6

In the example, shown above, instanceof operator compares the instance with type of person class. Hence, p is an instance of Person class due to which it returns true.
 
Another example of instanceof operator with inheritance is given below.
 
Code
  1. class Car {}  
  2. public class Swift extends Car {  
  3.     public static void main(String args[]) {  
  4.         Swift s = new Swift();  
  5.         System.out.println(s instanceof Car);  
  6.     }  
  7. }  
7

Output

8

In the example, mentioned above, Swift class is a child class and Car is a parent class. Both class have IS-A relationship, then the object of Swift class can be referred by either Swift or Car class. Therefore, it return true.
 
If variable have null value

In Java, instanceof operator with a variable has a null value.
 
Let’s see an example, given below.
 
Code
  1. public class Person {  
  2.     public static void main(String args[]) {  
  3.         Person p = null;  
  4.         System.out.println(p instanceof Person);  
  5.     }  
  6. }  
9

Output

10

Instanceof operator with Downcasting

If child class type refers to the object of parent class, it is called as a downcasting. We can’t perform it directly in Java. Java does not allow the concept of downcasting and the compiler gives the compile time error.
 
If we perform it by typecasting, it gives a runtime exception (ClassCastException).
 
But downcasting is possible if we use instanceof operator.
 
Let’s see an example of downcasting, given below.
 
Code
  1. class Car {  
  2.     void run() {  
  3.         System.out.println("Running fast");  
  4.     }  
  5. }  
  6. public class Swift extends Car {  
  7.     public static void main(String args[]) {  
  8.         Swift s = new Car(); // Compile time error.  
  9.     }  
  10. }  
11

In the example, mentioned above, Downcasting is not allowed in Java due to which the compiler gives an error.
 
Let’s see an example of downcasting by typecasting, given below.
 
Code
  1. class Car {  
  2.     void run() {  
  3.         System.out.println("Running fast");  
  4.     }  
  5. }  
  6. public class Swift extends Car {  
  7.     public static void main(String args[]) {  
  8.         Swift s = (Swift) new Car(); // it thrown runtime error.  
  9.     }  
  10. }  
12

Output

13

In the example, shown above, downcasting by typecasting gives a runtime error (ClassCastException).
 
Now, let’s see an example of downcasting with instanceof operator, given below.
 
Code
  1. class Car {}  
  2. public class Swift extends Car {  
  3.     static void run(Car c) {  
  4.         if (c instanceof Swift) {  
  5.             Swift s = (Swift) c; //downcasting  
  6.             System.out.println("Downcasting");  
  7.         }  
  8.     }  
  9.     public static void main(String args[]) {  
  10.         Car c = new Swift();  
  11.         Swift.run(c);  
  12.     }  
  13. }  
14
Output

16

In the example, shown above, downcasting is possible with the use of instanceof operator.
 
Downcasting without Instanceof operator

Downcasting is performed without the use of instanceof operator.
 
Let’s see an example, given below.
 
Code
  1. class Car {}  
  2. public class Swift extends Car {  
  3.     static void run(Car c) {  
  4.         Swift s = (Swift) c; //downcasting  
  5.         System.out.println("Downcasting");  
  6.     }  
  7.   
  8.     public static void main(String args[]) {  
  9.         Car c = new Swift();  
  10.         Swift.run(c);  
  11.     }  
  12. }  
17

Output

18

In the example, shown above, actual object is referred by c, which is an object of Swift class. Due to this, if we downcast it, it does not gives any type of error.
 
If we write like.
  1. Car c = new Car();  
  2. Swift.run(c);  
This time, it gives a runtime exception (ClassCastException) but when we use instanceof operator, it does not give any type of error.
 
Summary

Thus, we learnt that instanceof operator is used to compare the object is an instance of specific class or subclass or an interface and also learnt how to use it in Java.