Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

How to check if a Object is convertable to String?

Thread view: 
Arnold Peters - 16 Feb 2005 00:46 GMT
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
Hal Rosser - 16 Feb 2005 01:06 GMT
> I have e.g. a declaration like:
>
[quoted text clipped - 5 lines]
>
> Arni

whenever assigning an object to a String variable - isn't the 'toString'
method called automatically ?
- object has the toString method - so any object can be assigned to a
String.
Ryan Stewart - 16 Feb 2005 01:28 GMT
> whenever assigning an object to a String variable - isn't the 'toString'
> method called automatically ?
No

> - object has the toString method - so any object can be assigned to a
> String.
You can get the "String representation" of any object. Whether that
representation is meaningful or not depends on the object.
cloud9 - 16 Feb 2005 03:16 GMT
offcourse u can display meaningful message by overrdidng toString
method in that class
> > whenever assigning an object to a String variable - isn't the 'toString'
> > method called automatically ?
[quoted text clipped - 4 lines]
> You can get the "String representation" of any object. Whether that
> representation is meaningful or not depends on the object.
Boudewijn Dijkstra - 16 Feb 2005 20:51 GMT
> offcourse u can display meaningful message by overrdidng toString
> method in that class

Only if you have access to the source of that class.

>> > whenever assigning an object to a String variable - isn't the
> 'toString'
[quoted text clipped - 6 lines]
>> You can get the "String representation" of any object. Whether that
>> representation is meaningful or not depends on the object.
Lee Fesperman - 16 Feb 2005 01:07 GMT
> I have e.g. a declaration like:
>
[quoted text clipped - 3 lines]
> 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.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Eric Sosman - 16 Feb 2005 14:50 GMT
> I have e.g. a declaration like:
>
[quoted text clipped - 3 lines]
> 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?

   You might be asking for this:

    String string = myobj.toString();

... or you might be asking for this:

    String string = null;
    if (myobj instanceof String)
       string = (String)myobj;

... or you might be asking for something else I'm unable
to imagine -- if so, please explain more fully.

Signature

Eric.Sosman@sun.com

Nigel Wade - 16 Feb 2005 16:16 GMT
> I have e.g. a declaration like:
>
[quoted text clipped - 3 lines]
> 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?

Every class inherits the toString() method from Object. So you can assign a
representation of any object to a String by:

String str = anyObject.toString();

However, unless the class overrides the toString() method to return
something useful all you will get is the class name and a string
representation of the objects hash code - exactly what you would get if you
executed System.out.println(anyObject) which uses toString() implicitly.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.