Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.

As the name suggests, instanceof in Java is used to check if the specified object is an instance of a class, subclass, or interface. It is also referred to as the comparison operator because of its feature of comparing the type with the instance. Generally, you will only receive true or false as a result of the instanceof operator. It is common to use the instanceof operator for the following:

  • Checking if the given variable contains an object instance or not
  • Checking if typecasting is valid or not
  • Downcasting (when the parent object is referred by the subclass type)

Instanceof in Java Example

Now that you know what instanceof in Java is, try to understand it better with an example:

InstanceofinJavaExample

As you can see in the example depicted above, instanceof in Java is being used to check if the object e is an instance of the Example1 class. Since object e is present in the Example1 class, the output of the program is true.

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

More Examples of Using the Instanceof Operator

Since a subclass extends a parent class, the object reference in the subclass is also an instance of the parent class through inheritance. Please have a look at the below example that demonstrates it.

InstanceofinJavaExample2

As you can see in the example above, the class Mercedes extends the class Car. This means that Car is the parent class and Mercedes is the child class. The demo refers to the child class’s object and checks if it is an instance of the parent class. The output of the program is true. This means that the object of the subclass is also an instance of the parent class. It will return true again when checked with the object because the latter is an ancestor of all classes in Java. Let’s see another example that includes a subclass, parent class, and object, and see how the instanceof works in Java.

InstanceofinJavaExample3

The example stated above shows the use of instanceof in Java and how it can check the instance regarding any object.

Applying Instanceof with a Variable that has a Null Value

Applying instanceof in Java with a variable that has a null value will return false. In the code below, you will see the use of the instanceof operator with a null value variable. Scroll down and see what it returns.

ApplyingInstanceof

Applying Instanceof Operator with Different Parent-Child References

If you apply the instanceof operator to a parent object, you will get false. But if you use a parent reference referring to a child, it will return true. In the below example applies instanceof in Java to both scenarios. And this is what the result is.

ApplyingInstanceof2

Create and Showcase Your Portfolio from Scratch!

Caltech PGP Full Stack DevelopmentExplore Program
Create and Showcase Your Portfolio from Scratch!

Using Instanceof in Java for Downcasting

One of the primary applications of Java instanceof operator is downcasting. It is an act of using a subclass type to refer to a parent object. In simpler terms, it means typecasting a parent’s object to a child’s object. Although downcasting can also be done without an instanceof operator’s help, there is a considerable possibility of encountering compile-time or run-time errors. Hence, it is always a good practice to use the Java instanceof operator to see if downcasting is valid or not. The below example shows the use of instanceof in Java for downcasting.

UsingInstanceof

Conclusion

With this article, you have explored the use of instanceof in Java. You can refer to our Java tutorial for beginners article for understanding more such concepts. Java is a popular programming language worldwide. If you want to become a software developer, then you can opt for Simplilearn’s Post Graduate Program in Full Stack Web Development. It offers you a vast range of training materials with 60 hours of applied learning, to help you excel in the field of Java development.

Do you have any questions for us? Mention them in the comments section of this article, and our experts will get back to you at the earliest!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.