This operator is used only for object reference variables.
The operator checks whether the object is of a particular type (class type or interface type). instance of operator is
wriiten as:
(Object reference variable ) instanceof (class/interface type)
If the object referred by the variable on the left side of
the operator passes the IS-A check for the class/interface type on the right side, then the result will be true.
Following is the example:
String name = “James”;
boolean result = name instanceof
String;
// This will return true since name is type of String
This operator will still return true if the object being
compared is the assignment compatible with the type on the right.
Following is one more example:
classVehicle{}
public class CarextendsVehicle{
public static void main(String
args[]){
Vehicle a =newCar();
boolean result = a instanceof Car;
System.out.println(result);
}
}
This would produce the following result:
True
No comments:
Post a Comment
Please write your view and suggestion....