jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "casting" Watch "casting" New topic


peter brews
Ranch Hand

Joined: Jan 25, 2002
Posts: 59
Could you please tell what is right way of casting an object to string.
so that i can do string functions like checking for null,equality,contains etc..

Gale Greaser

Joined: Aug 05, 2002
Posts: 17
Say "obj" is an Object that can be represented as a String.
Then you could use either of these:
String objectString1 = "" + obj;
String objectString2 = String.valueOf(obj);
Good luck!
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
See http://jinx.swiki.net/77
Also notice that we are talking about String *conversion* - there is no casting involved at all.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
You may want to strongly consider overriding the 'equals' (and of course hash) methods for your object rather than converting it to a String. String equality checks are comparitively slow and may not exhibit the behaviour you require.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Jeremy is right! I should have read the whole question, I guess... :roll:
I agree. Here's the link: http://aspose.com/file-tools
subject: casting
It's not a secret anymore!