In Java, instanceof is an operator which is used to check whether the given object is a particular instance type or not. It returns either True or False. If the given object corresponds to the given particular type, this operator will return true, else it will return false.

The Java instanceof operator is also known as comparison operator as it compares the type of an object.

Basic Functionality of instanceof operator:

Let us see the basic example for instanceof operator

public class Instanceof_Demo {
    public static void main(String[] args) {
        String str = "onlinetutorialspoint";
        System.out.println("Is str instanceof String type ? " + (str instanceof String));
    }
}

Output:

Is str instanceof String type ? true

Comparing Different Objects with Java instanceof :

By using the instanceof operator, we can compare two different objects. But the important point here is, the two objects should be the same type i.e., there should be some relation between the two objects (either child to parent or parent to child or same type) otherwise the compiler will generate a compile-time error.

Example for Java instanceof with different objects :

java instanceof

In the above diagram, the java.lang.Thread is a class which implements the java.lang.Runnable interface and it also extends from java.lang.Object. So we can compare the Thread class with both the Object type and Runnable type.

Code:

public class InstanceOf_Relation {
    public static void main(String[] args) {
        Thread thread = new Thread();
        System.out.println("is thread instanceof Thread ? " + (thread instanceof Thread));
        System.out.println("is thread instanceof Runnable ? " + (thread instanceof Runnable));
        System.out.println("is thread instanceof Object ? " + (thread instanceof Object));
    }
}

Output:

is thread instanceof Thread ? true
is thread instanceof Runnable ? true
is thread instanceof Runnable ? true

Invalid comparison with java instanceof :

instanceof example wrong

On the above diagram, there is a relation between the java.lang.String to java.lang.Object and java.lang.Thread to java.lang.Object. But there is no relation between java.lang.String to Java.lang.Thread. If use the instanceof operator for these types, we will get compilation error called incompatible types.

Example for Wrong Comparison :

public class InstanceOf_Relation {
    public static void main(String[] args) {
        Thread thread = new Thread();
        System.out.println("is thread instance of String ? " + (thread instanceof String));
    }
}

Output:

InstanceOf_Relation.java:6: error: incompatible types: Thread cannot be converted to String
System.out.println("is thread instance of String ? "+(thread instanceof String));
^
1 error

The above example generates compilation error because there is no relationship between the Thread and String classes. To use Java instanceof, there should be a relation between the two arguments.

Relationship between Inheritance and Java instanceof operator :

We can use the instanceof operator to check the type of an abject in a child to parent relationship also.

If we check the child class object with the parent class type we get the output as true since the child class is extended from the parent class. But whenever we check the parent class object with the child class type by using instanceof operator, then we will always get false as the output as the parent class is the base class.

Example for instanceof operator and Inheritance Relationship :

class Person {}
class Employee extends Person {
    public static void main(String[] args) {
        Employee e = new Employee();
        Person p = new Person();
        System.out.println("is Employee Person Type ? " + (e instanceof Person));
        System.out.println("is Person Employee Type ? " + (p instanceof Employee));
        Object object = new String("onlinetutorialspoint");
        System.out.println("is object instance of String ? " + (object instanceof String));
    }
}

Output :

is Employee Person Type ? true
is Person Employee Type ? false
is object instance of String ? true

instanceof operator on null values:

If we apply the type checking with instanceof operator on null values, the result is always false.

class Instanceof_null {
    public static void main(String[] args) {
        System.out.println(null instanceof String);
        System.out.println(null instanceof Thread);
        System.out.println(null instanceof InstanceOf_null);
        
    }
}

Output :

 false
false
false

On the above example, we compared null with the String, Thread and Instanceof_null objects. For all comparisons, we got false as the output.

Hence in this tutorial, we learnt about instanceOf operator and its uses.

Happy Learning 🙂