What is Instanceof Java Operator?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

What is instanceof Java Operator?

In Java, the instanceof operator is used to determine whether an instance belongs to a particular type (class, subclass, or interface). It returns true or false as other comparison operators do.

Because it compares the instance with type, the instanceof operator in Java is also known as a type comparison operator. Any variable that has a null value when the instanceof operator is used returns false.

Also Read- Complete List of Operators in Java

Syntax

Syntax of instanceof in java is given below:

How does instanceof Java Operator Work?

The is-a connection is the foundation upon which the instanceof operator operates. A class inheritance or interface implementation serves as the foundation for the idea of an is-a connection. To demonstrate this, we'll create a Shape interface:

We will create a class Circle that will implement the Shape interface

The instanceof result will be true if the object is an instance of the type:

If the type is an interface, it will return true if the object implements the interface:

So instanceof will return true if the object is an instance of the specified type (class or subclass or interface).

How to Use Java instanceof operator with Objects?

In this example, we are going to see how to use instanceof with objects.

Output:

Explanation:

In the above code we have created an object of Test class and then checked if the testObj is an instance of type Test so it will return true.

How to use instanceof Java Operator When the Object is Null?

The instanceof operator returns false when used on a variable that has no value. Take a look at the example below where the instanceof operator is used with a variable that has no value.

Output:

Explanation:

  • In the above code, we have created an object of Test class with null value and named it testObj again.
  • Then we have checked if testObj is an instance of Test type this will return false as the instanceof operator returns false when used on a variable that has no value.

How to use instanceof Java Operator with Downcasting?

Downcasting occurs when a subclass type refers to an object of the parent class. If we try to execute it immediately, the compiler returns an error. A ClassCastException is thrown at runtime if you typecast it. However, downcasting is doable if we utilize the instanceof operator.

Assume that Animal class is the parent class of Dog class. Then the following statement throws compilation error.

If we perform downcasting by typecasting, ClassCastException is thrown at runtime.

Downcasting with instanceof

Output:

Explanation:

  • In the above code, we have created a method called method() which will downcast instance a of type Animal to Dog.
  • By using instanceof we can perform downcasting without throwing any exceptions.

instanceof Java Example

In this example, we are going to see how to use instanceof in Java to check if one object belongs to a particular type or not.

Output:

Explanation:

  • In the above code, we have created two classes: Parent and Child.
  • We have created an object of Child class namely child_obj and checked if it is an instance of itself, its parent class, and the Object class which is extended by all classes.

Conclusion

  • In Java, the instanceof comparison operator is used to determine whether an object instance belongs to a particular type (class, subclass, or interface).
  • It returns true or false similar to other comparison operators.
  • The instanceof operator in Java is also known as a type comparison operator.
  • Downcasting can be performed by instanceof in java without throwing any runtime exceptions.
  • If we use the instanceof operator with a variable having a null value instanceof will always return false.