How to check if a Object is convertable to String?

A

Arnold Peters

I have e.g. a declaration like:

Object myobj = ...<unknown>...

Now I want to assign it to a String. For most possible Objects it works
but sometimes a Object could occur for which a conversion is not possible.
How do I detect in advance (without using a try ... catch clause) if it is possible?

Arni
 
L

Lee Fesperman

Arnold said:
I have e.g. a declaration like:

Object myobj = ...<unknown>...

Now I want to assign it to a String. For most possible Objects it works
but sometimes a Object could occur for which a conversion is not possible.
How do I detect in advance (without using a try ... catch clause) if it is possible?

In general, calling the toString() method is the best way:

String myString = myobj.toString();

If the object is actually a String, toString() will return itself.

I have a feeling this is not the answer you're looking for. In that case, please provide
more information, such as the code you wish to use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top