ProAnswers.org

How to Check whether the object referenc was of type String?

class Animal {
public Animal(String aType) {
type = aType;
}
public String toString() {
return "This is a " + type;
}
private String type;
}
public class MainClass {
public static void main(String[] a) {
Animal pet = new Animal(β€œa”);
if (pet.getClass() == Animal.class) {
System.out.println(β€œit is an animal!”);
}
}
}