C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

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

0 votes
106 views
asked by smith Geek (15,340 points)

1 Answer

0 votes
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!");
    }
  }
}
answered by sunny Expert (11,410 points)

Related questions

0 votes
2 answers 76 views
76 views asked by augustaina Sr Member (4,820 points)
0 votes
1 answer 20 views
20 views asked by keem Expert (13,240 points)
0 votes
1 answer 53 views
0 votes
1 answer 37 views
37 views asked by keem Expert (13,240 points)
0 votes
1 answer 10 views
10 views asked by john Guru (27,140 points)
...