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:
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.
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.
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.
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.
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.
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.
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!