The instanceof
operator in Java is used to check whether an object is an instance of a particular class or not.
Its syntax is
objectName instanceOf className;
Here, if objectName is an instance of className, the operator returns true
. Otherwise, it returns false
.
Example: Java instanceof
class Main {
public static void main(String[] args) {
// create a variable of string type
String name = "Programiz";
// checks if name is instance of String
boolean result1 = name instanceof String;
System.out.println("name is an instance of String: " + result1);
// create an object of Main
Main obj = new Main();
// checks if obj is an instance of Main
boolean result2 = obj instanceof Main;
System.out.println("obj is an instance of Main: " + result2);
}
}
Output
name is an instance of String: true obj is an instance of Main: true
In the above example, we have created a variable name of the String
type and an object obj of the Main class.
Here, we have used the instanceof
operator to check whether name and obj are instances of the String
and Main class respectively. And, the operator returns true
in both cases.
Note: In Java, String
is a class rather than a primitive data type. To learn more, visit Java String.
Java instanceof during Inheritance
We can use the instanceof
operator to check if objects of the subclass is also an instance of the superclass. For example,
// Java Program to check if an object of the subclass
// is also an instance of the superclass
// superclass
class Animal {
}
// subclass
class Dog extends Animal {
}
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog d1 = new Dog();
// checks if d1 is an instance of the subclass
System.out.println(d1 instanceof Dog); // prints true
// checks if d1 is an instance of the superclass
System.out.println(d1 instanceof Animal); // prints true
}
}
In the above example, we have created a subclass Dog that inherits from the superclass Animal. We have created an object d1 of the Dog class.
Inside the print statement, notice the expression,
d1 instanceof Animal
Here, we are using the instanceof
operator to check whether d1 is also an instance of the superclass Animal.
Java instanceof in Interface
The instanceof
operator is also used to check whether an object of a class is also an instance of the interface implemented by the class. For example,
// Java program to check if an object of a class is also
// an instance of the interface implemented by the class
interface Animal {
}
class Dog implements Animal {
}
class Main {
public static void main(String[] args) {
// create an object of the Dog class
Dog d1 = new Dog();
// checks if the object of Dog
// is also an instance of Animal
System.out.println(d1 instanceof Animal); // returns true
}
}
In the above example, the Dog class implements the Animal interface. Inside the print statement, notice the expression,
d1 instanceof Animal
Here, d1 is an instance of Dog class. The instanceof
operator checks if d1 is also an instance of the interface Animal.
Note: In Java, all the classes are inherited from the Object
class. So, instances of all the classes are also an instance of the Object
class.
In the previous example, if we check,
d1 instanceof Object
The result will be true
.
Also Read: