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

abstract keyword in Java

boolean Keyword in Java

break keyword in Java

Byte keyword in Java 

case keyword in Java 

catch keyword in Java 

char keyword in Java 

class keyword in Java 

continue keyword in Java 

default keyword in Java 

do keyword in Java

double keyword in Java

else keyword in Java

enum keyword in Java

extends keyword in Java

final keyword in Java

finally keyword in Java

float keyword in Java

for keyword in Java

if keyword in Java

static keyword in Java

super keyword in Java

 

Suggested for you

Usage of this statement in the Java language

Keywords in Java language

Keywords in C language

Keywords in Python language

 

 

The transient keyword in Java language
Final keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago