The instanceof operator in Java programming language
- Home
- Keyword in Java
- The instanceof operator in Java programming language
- On
- By
- 2 Comments
- Categories: Keyword in Java
The instanceof operator in Java programming language
The instanceof operator in Java programming language
In this tutorial, we will discuss The instanceof operator in Java programming language.
In Java programming language, the instanceof is a keyword or operator is used to check the type of object at runtime. The instanceof operator helps to compare the instance with type and it returns either as true or false.
What is the instaceof
Test whether the reference variable on the left is an instance /object of the specified type (class or subclass or interface) on the right.
Reference_Variable instanceof class_name/interface_name
Example
Sample s=new Sample();
System.out.println(s instanceof Sample);
The simple instance of operator
program 1
class Sim_ope{ //class with class name public static void main(String args[]){ //main metod Sim_ope s=new Sim_ope();//object for class System.out.println(s instanceof Sim_ope);//true } }
When the above code is executed, it produces the following results:
true
Example for the instanceof operator with inheritance
Program 2
Here, its a very simple example for instanceof operator where it test the present class and it returns boolean expression(true/false)
class My_vehicle{ } //class with class name class My_bus extends My_vehicle{ //My_bus inherits My_vehicle public static void main(String args[]){ //main metod My_vehicle s=new My_vehicle();//object for class System.out.println(s instanceof My_vehicle);//true } }
When the above code is executed, it produces the following results:
true
instanceof with null
program 3
When we apply instanceof operator with a variable that assigned null value, it returns false
Here, another example
class Test_ope{ public static void main(String args[]){ Test_ope t=null; System.out.println(t instanceof Test_ope); } }
When the above code is executed, it produces the following results:
false
Program 4
class grand_parent{}//class grand parent class parent extends grand_parent{}//class parent inherits child class child extends parent{}//class child inherits parent class test_class{ public static void main(String args[]){ child obj=new child(); if(obj instanceof child){//instance of return true of child class System.out.println("obj is instance of child"); } else{ System.out.print("obj is not instance of child"); } if(obj instanceof parent){//instance of return true also parent class System.out.println("obj is instance of parent"); } else{ System.out.println("obj is not instance of parent"); } if(obj instanceof grand_parent){//instance of return true also grand parent class System.out.println("obj is instance of grand parent"); } else{ System.out.print("obj is not instance of grand parent"); } } }
When the above code is executed, it produces the following results:
obj is instance of child obj is instance of parent obj is instance of grand parent
There are other Java language keywords that are similar to this keyword
float keyword in Java
Suggested for you
Usage of this statement in the Java language
2 Comments
andriller license key July 1, 2019 at 11:17 pm
I truly enjoy looking through on this web site , it holds superb content .
Karmehavannan July 6, 2019 at 6:46 am
thank you