The instanceof operator is a commonly used operator in Java. It is used to check whether an object is an instance of a particular class or a subclass of that class. In this article, we will explore the instanceof operator and how it is used in Java.
The syntax of instanceof operator
The instanceof operator is a binary operator that is used to check whether an object is an instance of a particular class or a subclass of that class. The syntax of the instanceof operator is as follows:
object instanceof class
Here, object is the object to be tested, and class is the class or interface being tested for. The instanceof operator returns a boolean value: true if object is an instance of class, or false otherwise.
Examples of instanceof operator
Here are some examples of the instanceof operator:
String str = "Hello, world!";
if (str instanceof String) {
System.out.println("str is an instance of String class.");
}
In this example, we create a String object and then use the instanceof operator to check whether it is an instance of the String class. Since str is an instance of the String class, the if statement will evaluate to true.
Object obj = new Integer(10);
if (obj instanceof Integer) {
System.out.println("obj is an instance of Integer class.");
}
In this example, we create an Object reference and assign it an Integer object. Then, we use the instanceof operator to check whether the object referenced by obj is an instance of the Integer class. Since obj is an instance of the Integer class, the if statement will evaluate to true.
Object obj = new Integer(10);
if (obj instanceof Number) {
System.out.println("obj is an instance of Number class or its subclass.");
}
In this example, we create an Object reference and assign it an Integer object. Then, we use the instanceof operator to check whether the object referenced by obj is an instance of the Number class or its subclass. Since Integer is a subclass of Number, the if statement will evaluate to true.
When to use instanceof operator
The instanceof operator is typically used in situations where we want to perform a particular action based on the type of an object. For example, if we have a collection of objects of different types, we can use the instanceof operator to determine the type of each object and perform a specific action based on its type.
Here is an example:
List<Object> list = new ArrayList<>();
list.add("Hello, world!");
list.add(new Integer(10));
for (Object obj : list) {
if (obj instanceof String) {
System.out.println("String: " + obj);
} else if (obj instanceof Integer) {
System.out.println("Integer: " + obj);
}
}
In this example, we create a list of objects that includes a String and an Integer. Then, we loop through the list and use the instanceof operator to determine the type of each object and print its value.
Conclusion
The instanceof operator is a useful operator in Java that allows us to check whether an object is an instance of a particular class or a subclass of that class. It is commonly used in situations where we need to perform a particular action based on the type of an object.
convert int to string in java difference between c++ and java difference between jdk jre and jvm how to install java inheritance in java install java instanceof java instanceof operator in java instanceof operator java int to string in java int to string java java substring jdk jre jvm string to int java substring in java super keyword java