Java instanceof
Operator
The instanceof
operator is used in Java to test if an object is an instance of a particular class or one of its subclasses. The operator returns a boolean value (true
or false
) based on whether or not the object is an instance of the specified class or any of its subclasses.
Syntax
The instanceof
operator has the following syntax:
object instanceof class
where object
is the object to be tested and class
is the class or interface being tested against.
Example
Here is an example that demonstrates the use of the instanceof
operator:
public class Example {
public static void main(String[] args) {
Object obj1 = new String("Hello");
Object obj2 = new Integer(10);
System.out.println(obj1 instanceof String); // true
System.out.println(obj1 instanceof Object); // true
System.out.println(obj2 instanceof Integer); // true
System.out.println(obj2 instanceof Number); // true
System.out.println(obj2 instanceof Object); // true
System.out.println(obj1 instanceof Integer); // false
System.out.println(obj2 instanceof String); // false
}
}
In this example, we create two objects obj1
and obj2
of type String
and Integer
, respectively. We then test these objects using the instanceof
operator.
Output
The output of the above code will be:
true
true
true
true
true
false
false
Explanation
In the first System.out.println()
statement, we test if obj1
is an instance of the String
class. Since it is, the result is true
.
In the second System.out.println()
statement, we test if obj1
is an instance of the Object
class. Since String
is a subclass of Object
, the result is true
.
In the third to fifth System.out.println()
statements, we test if obj2
is an instance of the Integer
, Number
, and Object
classes, respectively. Since Integer
extends Number
which extends Object
, all three tests result in true
.
In the sixth and seventh System.out.println()
statements, we test if obj1
is an instance of the Integer
class and if obj2
is an instance of the String
class, respectively. Since neither of these objects are of the specified class or its subclass, both tests result in false
.
Use
The instanceof
operator is often used in Java to ensure the proper handling of objects in a program. It is used, for example, in an if
statement to check if an object is of a particular class before casting it to that class. This helps to prevent runtime errors that occur when a cast is attempted on an object of the wrong class.
Important Points
- The
instanceof
operator is used to test if an object is an instance of a particular class or one of its subclasses. - The operator returns a boolean value (
true
orfalse
) based on the object's class or one of its subclasses. - The
instanceof
operator is often used to ensure the proper handling of objects in a program.
Summary
The instanceof
operator is a useful tool in Java that allows for the testing of an object's class or one of its subclasses. It is used to ensure proper handling of objects in a program and to prevent runtime errors that occur when casts are attempted on objects of the wrong class.