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.
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);
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
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
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
Usage of this statement in the Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
View Comments
I truly enjoy looking through on this web site , it holds superb content .
thank you