Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Casting objects

Is it possible to find out what type of object an Object is...For example I use an ObjectOutputStream to serialize an Object across a network, however this 'serializer' could be serializing Strings, HashMaps, basically any datastructre. Is there a way when the Object is deserialized at the other end to tell what sort of Object it is? How to I Cast it back to its proper type?
0
matjc
Asked:
matjc
1 Solution
 
bobbit31Commented:
Object o;

if (o.getClass() == String.class) {
  String s = (String) o;
}
0
 
allahabadCommented:
String className = object.getClass(); // to get the class name of the object.

Suppose that you got class name "java.util.Vector"
Vector vector = (java.util.Vector) object;
0
 
fivesigmaeventCommented:
the instanceof operator can be used to compare for what an object is.

if( o instanceof String )
{ ... }
else if( o instanceof HashMap )
{ ... }

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ksivananthCommented:
If you have choices to check, you can use the methods above. but without choice, I don't think its possible
0
 
CEHJCommented:
String className = getClass().getName();
0
 
matjcAuthor Commented:
allahabad,

getClass method actually return an object of type Class so the example given doesnt work however its an insteresting start, unfortunatly you are unable to do the following:

String className = object.getClass().toString;

className myDataStructure = (className)object

using className as the type, this would solve the issue of not necesserily knowing what type of object you are receiving.

any more ideas?
0
 
CEHJCommented:
>>getClass method actually return an object of type Class

that's why i posted the correct code ;-)
0
 
matjcAuthor Commented:
CEHJ,

>>that's why i posted the correct code ;-)

I typed my comment as you submitted yours I guess, anyway any idea how to solve the comment about casting an object to the correct type without knowing in the first instance what type it is?

calling .toName() as apposed to .toString() does not really change the question
0
 
CEHJCommented:
>>any idea how to solve the comment about casting an object to the correct type without knowing in the first instance what type it is?

You can't really, but this begs the question of why you would want to do this - i.e. - what are you trying to do?
0
 
matjcAuthor Commented:
not worth splitting the points, as I have implemented something similar to the accepted answer I have awarded the points to them, it was after all the only code fragment that worked here.
0
 
CEHJCommented:
>>it was after all the only code fragment that worked here.

I'm not sure what you mean by that

You could, of course, use the answer you chose

>>to find out what type of object an Object is...<<

as per your question if you are prepared to write an infinite series of

if (o instanceof X)

statements for every possible class in existence now, or at any time in the future. But not otherwise.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now