I have read all the answers which have been posted so far but couldn't find satisfactory answer yet. Answering to Is there some kind of statement like instanceof
for this check? or I should use qux.getClass().equals(Foo.class)
question I would say yes, there is instanceof
operator in java to check if the object is instance of class. Below is an example-:
class Vehicle {
}
class Car extends Vehicle {
}
public class Research {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
if (vehicle instanceof Vehicle) {
System.out.println("vehicle instanceof Vehicle : TRUE");
} else {
System.out.println("vehicle instanceof Vehicle : FALSE");
}
if (vehicle instanceof Car) {
System.out.println("vehicle instanceof Car : TRUE");
} else {
System.out.println("vehicle instanceof Car : FALSE");
}
System.out.println();
Car car = new Car();
if (car instanceof Vehicle) {
System.out.println("car instanceof Vehicle : TRUE");
} else {
System.out.println("car instanceof Vehicle : FALSE");
}
if (car instanceof Car) {
System.out.println("car instanceof Car : TRUE");
} else {
System.out.println("car instanceof Car : FALSE");
}
}
}
Output-:
vehicle instanceof Vehicle : TRUE
vehicle instanceof Car : FALSE
car instanceof Vehicle : TRUE
car instanceof Car : TRUE
Description-:instanceof
operator tells if an object is a instance of a class or it's parent classes (up to any level).
vehicle instanceof Car : FALSE
line of output is indicating that instanceof
operator will not tell if an object is a instance of its sub class.
Another way is to use getClass().equals(Foo.class)
to determine if an object is a instance of a class or not. Let us see below example-:
class Vehicle {
}
class Car extends Vehicle {
}
public class Research {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
if (vehicle.getClass().equals(Vehicle.class)) {
System.out.println("vehicle instanceof Vehicle : TRUE");
} else {
System.out.println("vehicle instanceof Vehicle : FALSE");
}
if (vehicle.getClass().equals(Car.class)) {
System.out.println("vehicle instanceof Car : TRUE");
} else {
System.out.println("vehicle instanceof Car : FALSE");
}
System.out.println();
Car car = new Car();
if (car.getClass().equals(Vehicle.class)) {
System.out.println("car instanceof Vehicle : TRUE");
} else {
System.out.println("car instanceof Vehicle : FALSE");
}
if (car.getClass().equals(Car.class)) {
System.out.println("car instanceof Car : TRUE");
} else {
System.out.println("car instanceof Car : FALSE");
}
}
}
Output-:
vehicle instanceof Vehicle : TRUE
vehicle instanceof Car : FALSE
car instanceof Vehicle : FALSE
car instanceof Car : TRUE
Description-: It would be clear from the above example that which one(out of above two) should be opted where?
Important Notes-:
instanceof
operator will not throw NullPointerException
exception in case if reference variable is not pointing to any object(i.e. its having null reference).
car.getClass().equals(Car.class)
will throw NullPointerException
exception in case if car
is not pointing to any object(i.e. its having null reference). Therefore one must place extra null check with this for example car != null && car.getClass().equals(Car.class)
to prevent it from NullPointerException
.
instanceof
operator tells if an object is a instance of a class or it's parent classes (up to any level).
car.getClass().equals(Car.class)
will tell if an object is a instance of class only. (Parent & Sub classes will not be considered at all)