File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

How do I indentify an object type ?

Shunjie Liu

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 ?

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 ?
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

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

I agree. Here's the link:
subject: How do I indentify an object type ?
It's not a secret anymore!