?
Solved

Casting objects

Posted on 2003-03-04
11
Medium Priority
?
332 Views
Last Modified: 2010-05-18
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
Comment
Question by:matjc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8068987
Object o;

if (o.getClass() == String.class) {
  String s = (String) o;
}
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8068988
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
 

Accepted Solution

by:
fivesigmaevent earned 150 total points
ID: 8069116
the instanceof operator can be used to compare for what an object is.

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

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 26

Expert Comment

by:ksivananth
ID: 8069555
If you have choices to check, you can use the methods above. but without choice, I don't think its possible
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8070773
String className = getClass().getName();
0
 

Author Comment

by:matjc
ID: 8070786
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8070794
>>getClass method actually return an object of type Class

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

Author Comment

by:matjc
ID: 8070817
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8070844
>>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
 

Author Comment

by:matjc
ID: 8073274
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8074746
>>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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question