Last Updated: 30 September, 2023
In Java, instanceof operator is used to check whether an object is an instance of a specific type (class or subclass or interface) or not. Because it compares the instance to the type, the instanceof operator is also known as the type comparison operator in Java.
It returns a boolean value as an output, true if the object is an instance of the specified type, and false otherwise.
An instanceof operator applied to a variable with a null value returns false.
Syntax:
objectName instanceOf className;
The following example shows how to use the instanceof operator to check if an object is an instance of a particular type:
Output
true
Output
Yes, appleObj is an instance of Apple.
Yes, appleObj is an instance of Fruit.
appleObj is instance of Object
Output
No, object is not an instance of Test
Output
NO, object is not an instance of Child
Output
YES, object is an instance of Child
The instanceof operator can be used in many ways in Java programming, as given below:
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com