instanceof keyword in java

The ” instanceof ” keyword is used to check whether an object is an instance of a particular class or interface

The instanceof in java is also known as type comparison operator because it compares the object or instance with type.It returns either true or false.

Example

  1. Object score = new Integer(98);
  2. if (score instanceof Integer) {
  3.          System.out.println("score is an integer");
  4. }
  5. else{
  6.  System.out.println("score is not an integer");
  7. }
Object score = new Integer(98);
if (score instanceof Integer) {
         System.out.println("score is an integer");
}
else{
 System.out.println("score is not an integer");
}


In the above example, score variable is holding reference to Integer object at run time and hence instanceof returns true.

  1. Object score = new Double(98.5);
  2. if (score instanceof Integer) {
  3.           System.out.println("score is an integer");
  4. }
  5. else{
  6.  System.out.println("score is not an integer");
  7. }
Object score = new Double(98.5);
if (score instanceof Integer) {
          System.out.println("score is an integer");
}
else{
 System.out.println("score is not an integer");
}


In the above example, score variable is holding reference to Double object at run time and hence instanceof returns false.

If we apply the instanceof operator with any variable that has null value, it returns false.

Example

  1. Object score = null;
  2. if (score instanceof Integer) {
  3.          System.out.println("score is an integer");
  4. }
  5. else{
  6.      System.out.println("score is not an integer");
  7. }
Object score = null;
if (score instanceof Integer) {
         System.out.println("score is an integer");
}
else{
     System.out.println("score is not an integer");
}


An object of subclass type is also a type of parent class.

For example, if Car extends Vehicle then object of Car can be referred by either Car or Vehicle class.

  1. class Vehicle{
  2. }  
  3. class Car extends Vehicle{//Car inherits Vehicle  
  4.  
  5.  public static void main(String args[]){  
  6.  Car c=new Car();
  7.  
  8. System.out.println(c instanceof Car);//true
  9.  
  10. System.out.println(c instanceof Vehicle);//true  
  11.  
  12.  }  
  13. }  
class Vehicle{
}  
class Car extends Vehicle{//Car inherits Vehicle  
  
 public static void main(String args[]){  
 Car c=new Car();

System.out.println(c instanceof Car);//true 

System.out.println(c instanceof Vehicle);//true  

 }  
}  


But the other way is not correct, An object of super class type is not a type of sub class.

Example

Object of Vehicle is not a type of Car as below

  1.  public static void main(String args[]){  
  2.        Vehicle v=new Vehicle();
  3.  
  4.        System.out.println(v instanceof Car);//false
  5.  
  6.        System.out.println(v instanceof Vehicle);//true  
  7.  
  8.     }  
  9. }
 public static void main(String args[]){  
       Vehicle v=new Vehicle();

       System.out.println(v instanceof Car);//false 

       System.out.println(v instanceof Vehicle);//true  

    }  
} 

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on