instanceof java

Instanceof Java with Example

In this tutorial, you will learn about the Java instanceof operator in-depth with example.

The instanceof operator in Java is used to determine whether an object is an instance of a given type (class or subclass or interface).

  • Because it compares the instance to the type, the instanceof operator in Java is also known as the type comparison operator. It either returns true or false. When we use the instanceof operator on a variable with a null value, we get false.
In Java, an instanceof is a comparison operator that checks whether an object instance is of a specified type (class/sub-class/interface). It returns true or false, just like other comparison operators.
When an object instance is compared to a null type using the instanceof operator, the result is false.

Syntax:

objectName instanceOf className;

The operator returns true if objectName is an instance of className. Otherwise, false is returned.

Basic example of java instanceof

Let’s look at a simple instance operator example that checks the current class.

class Sample{  
 public static void main(String args[]){  
 Sample d =new Sample();  
 System.out.println(d instanceof Sample);//true  
 }  
}  

Output:

true

A subclass type object is also a parent class type. For example, if the Sample extends Basic, then the object of Sample can be referred to by either the Sample or the Basic classes.

Example of Java instanceof during Inheritance

We can use the instanceof operator to see if a subclass object is also an instance of the superclass.

//superclass
class Sample {
}

// subclass
class Basic extends Sample {
}

class Main {
  public static void main(String[] args) {

    // create an object of the subclass
    Basic b = new Basic();

    // checks if b is an instance of the subclass
    System.out.println(d1 instanceof Basic);        // prints true

    // checks if b is an instance of the superclass
    System.out.println(b instanceof Sample);     // prints true
  }
}
  • In the preceding example, we created a subclass Basic that inherit from the superclass Sample. We’ve made a Basic class object called b.
  • Take note of the expression inside the print statement:
b instanceof Sample

In this case, we’re using the instanceof operator to see if b is also an instance of the superclass Sample.

Java instanceof in Interface

The instanceof operator is also used to determine whether an object of a class is also an instance of the interface that the class implements.

interface Sample {
}

class Basic implements Sample {
}

class Main {
  public static void main(String[] args) {

    // create an object of the Dog class
    Basic b = new Basic();

    // checks if the object of Basic
    // is also an instance of Sample
    System.out.println(b instanceof Sample);  // returns true
  }
}

The Basic class in the preceding example implements the Sample interface. Take note of the expression inside the print statement.

b instanceof Sample

In this case, b is an instance of the Basic class. The instanceof operator determines whether b is also an instance of the sample interface.

You may like:

Java this Keyword with Example

Hope this article will guide you to recognize all about the instanceof java that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has 2 Comments

Leave a Reply