*
The moose likes Beginning Java and the fly likes How do I indentify an object type ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I indentify an object type ?" Watch "How do I indentify an object type ?" New topic
Author

How do I indentify an object type ?

Shunjie Liu
Greenhorn

Joined: Feb 21, 2006
Posts: 7
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

Joined: Dec 13, 2006
Posts: 182
array[0].getClass() would give you the class of the object at 0th position.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3492
    
  13
If you know that it is either a String or an Integer you could use instanceof


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I indentify an object type ?