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
99 views
asked May 10, 2014 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 May 10, 2014 by sunny Expert (11,410 points)

Related questions

0 votes
2 answers 69 views
69 views asked May 10, 2014 by augustaina Sr Member (4,820 points)
0 votes
1 answer 19 views
19 views asked Jan 25, 2014 by keem Expert (13,240 points)
0 votes
1 answer 51 views
51 views asked Jan 9, 2014 by sara Guru (24,570 points)
0 votes
1 answer 35 views
35 views asked Feb 6, 2014 by keem Expert (13,240 points)
0 votes
1 answer 10 views
10 views asked Jan 9, 2014 by john Guru (27,140 points)
...