instanceof
in Java is an operator that is used to check if an object belongs to a particular class or a subclass of that class. It returns a boolean value of true
if the object is an instance of the specified class or a subclass of that class, and false
otherwise.
Here is the syntax of the instanceof
operator in Java:
object instanceof class
where object
is the object that you want to check and class
is the class or interface that you want to check for.
For example, if you have a class called Animal
and a subclass called Dog
, you can use the instanceof
operator to check if an object is an instance of either of these classes as follows:
Animal animal = new Animal();
Dog dog = new Dog();
if (animal instanceof Animal) {
System.out.println("animal is an instance of Animal");
}
if (dog instanceof Animal) {
System.out.println("dog is an instance of Animal");
}
if (dog instanceof Dog) {
System.out.println("dog is an instance of Dog");
}
In the above example, the first if
statement will evaluate to true
because animal
is an instance of the Animal
class. The second if
statement will also evaluate to true
because dog
is an instance of the Animal
class (since Dog
is a subclass of Animal
). The third if
statement will evaluate to true
because dog
is an instance of the Dog
class.
What is instanceof in Java programing language?
In Java programming language, "instanceof
" is an operator that is used to check whether an object is an instance of a particular class or not. It is a binary operator, which means it requires two operands to work.
The following is the simple syntax of instanceof
Java:
object instanceof class
Here, "object" is the object whose type is to be checked, and "class" is the name of the class that is being checked for. The operator returns a boolean value (true or false) depending on whether the object is an instance of the specified class or not.
When the instanceof
operator is used in a Java program, the following steps are taken:
- The object on the left-hand side of the operator is evaluated. This object can be a variable, an expression, or a literal value.
- The class on the right-hand side of the operator is evaluated. This class can be a specific class or an interface.
- The operator then checks if the object is an instance of the class. If the object is an instance of the class, the operator returns true. If the object is not an instance of the class, the operator returns false.
- The
instanceof
operator also returns true if the object is an instance of any subclass of the specified class. This is because a subclass is a type of its parent class, and so any object of the subclass is also an object of its parent class.
Now, let us go through various examples of instanceof
java to understand it fully:
Example-1:
public class Main {
public static void main(String[] args) {
String str = "Hello World";
if (str instanceof String) {
System.out.println("str is an instance of String class");
}
}
}
Output:
str is an instance of String class
In this example, we declare a String variable "str
" and initialize it with the value "Hello World". We then use the instanceof operator to check whether "str
" is an instance of the String class. Since "str
" is indeed a String object, the condition is true and the message "str is an instance of String class
" is printed to the console.
Example-2:
public class Main {
public static void main(String[] args) {
Object obj = new Integer(10);
if (obj instanceof Integer) {
Integer num = (Integer) obj;
System.out.println("obj is an instance of Integer class");
System.out.println("Value of obj: " + num);
}
}
}
Output:
obj is an instance of Integer class
Value of obj: 10
In this example, we create an Integer object "obj
" with a value of 10. We then use the instanceof
operator to check whether "obj
" is an instance of the Integer class. Since "obj
" is indeed an Integer object, the condition is true and we can safely cast "obj
" to an Integer object using the (Integer) casting syntax. We then print out a message to the console indicating that "obj
" is an instance of the Integer class, as well as its value.
Example-3:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Object> myList = new ArrayList<Object>();
myList.add("Hello");
myList.add(new Integer(10));
for (Object obj : myList) {
if (obj instanceof String) {
System.out.println(obj + " is a string");
} else if (obj instanceof Integer) {
System.out.println(obj + " is an integer");
} else {
System.out.println(obj + " is an unknown type");
}
}
}
}
Output:
Hello is a string
10 is an integer
In this example, we create an ArrayList of type Object and add two objects to it: a String object and an Integer object. We then iterate over the list using a for-each loop and use the instanceof
operator to check the type of each object. For the String object, the condition "obj instanceof String" is true and we print out a message indicating that it is a string. For the Integer object, the condition "obj instanceof Integer
" is true and we print out a message indicating that it is an integer. For any other type of object that may be added to the list in the future, we print out a generic message indicating that it is an unknown type.
Summary
instanceof
is a keyword operator in Java used to check if an object belongs to a specific class or subclass. It returns a boolean value of true
if the object is an instance of the specified class or a subclass of that class, and false
otherwise.
It is often used in conditional statements to determine if an object can be safely cast to a particular type before performing operations on it, avoiding ClassCastException
runtime errors.
The syntax of instanceof
is object instanceof class
, where object
is the object to be checked, and class
is the class or interface to be checked for.
Further reading