This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I indentify an object type ?

 
Shunjie Liu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I got a basic question.
How do I identify an object type without using generics in my code ?

Example
Object array = new Object[10];
String s = "hello";
Integer i = 5;
array[0] = s;
array[1] = i;

So now s and i are in the object array. Lets say I do not know their position. how do I tell if array[0] is a String or Integer object ?
Thanks
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
array[0].getClass() would give you the class of the object at 0th position.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know that it is either a String or an Integer you could use instanceof
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic