This operator checks whether the object is of a particular class/interface type. instanceof operator is written as:
( Object reference variable ) instanceof (class/interface type)
Example:
public class Test {
public static void main(String args[]){
String name = "Buyya";
// following will return true since name is type of String
boolean result = name instanceof String;
System.out.println( result );
}
}
This would produce the following result:
true
This operator will still return true if the object being compared is the assignment compatible with the type on the right.
Example:
class Vehicle {}
public class Car extends Vehicle {
public static void main(String args[]){
Vehicle a = new Car();
boolean result = a instanceof Car;
System.out.println( result );
}
}
This would produce the following result:
true