Java

Instance Of Operator in Java

In Java, the InstanceOf operator is referred to as the type comparison operator and is primarily practised to check the instance of an object in a class. When the object belongs to the specified type, instanceOf returns true; otherwise, false. Additionally, the instanceOf operator can also be used to check if the object belongs to an interface, child class, or parent class. This article provides the working of the instanceOf operator in Java.

How the instanceOf operator works

The working of the instanceOf operator depends on the following syntax:

(object) instanceOf (type)

The object in the above syntax refers to the variable. The type can be an interface or the class where the instance of the object is being checked.

How to use instanceOf operator

This section provides the working of the instanceOf operator by describing the examples. Each example refers to various applications of instanceOf operators in Java.

Example 1: Using instanceOf on a variable

Let’s start its basic application; the following code practices the use of instanceOf operator that checks the string variable belongs to the respective datatype or not:

package newpack;

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

        // string variable
        String x = "linuxhint";
       
        // using isntanceOf on string variable
        boolean res1 = x instanceof String;
        System.out.println("The result is:" +res1);

    }
}

The above code is explained below:

  • declaring a string variable named x
  • using instanceOf on x to check that it belongs to String type or not
  • printing the result

The output is shown below

Example 2: check the instance of an object in a class

We have created an object of a Java class and then checked its instance in the class using the following lines of code.

package newpack;

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

        // create an object of class InsOf
        InsOf ob = new InsOf();

        // using instanceOf operator on
        boolean r = ob instanceof InsOf;
        System.out.println("The result is: " + r);

    }
}

The following lines describe the code:

  • creates an object ob of class InsOf
  • applies instanceOf operator on ob object and stores its value in a Boolean variable(as the result would be true or false).
  • prints the result

The image of the output can be found here.

Example 3: Using instanceOf on a null variable

The default value of the instanceOf operator is false which comes into action when the variable contains nothing. Thus, if we declare a null variable and try to check its type using the instanceOf operator, a false output is returned. The following Java code shows the usage of the instanceOf operator on null value:

package newpack;

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

        // create an object of class InsOf
        InsOf a=null;

        // using instanceOf operator
        System.out.println(a instanceof InsOf);
    }
}

The output of the above code is shown in the following image:

Example 4: Using instanceOf operator in inheritance

This example provides the concept of instanceOf operator in Java class inheritance. The following code creates a subclass of the superclass and then the instanceOf operator is practiced:

package newpack;

public class Cars {}

//creating subclass of Cars
class BMW extends Cars {
    public static void main(String[]args) {
       
        //creating object of BMW
        BMW a= new BMW();
       
        //using instanceOf operator on (superclass)
        System.out.println(a instanceof Cars);
       
        //using instanceOf operator (subclass)
        System.out.println(a instanceof BMW);
    }
}

The description of the above code is:

  • creates a subclass named BMW
  • creates a new object
  • applies instanceOf operator on super-class
  • applies instanceOf operator on sub-class

The output of the code is provided below:

Conclusion

The instanceOf operator in Java is practiced checking whether the object is an instance of the class or not. Moreover, a variable can also be checked against its type using the instanceOf operator. This post describes the working of the instanceOf operator in Java. We have listed few examples that demonstrate the usage of the instanceOf operator in various perspectives.

About the author

Adnan Shabbir