| Author |
Type casting Strings
|
pavanasree vasireddy
Ranch Hand
Joined: Jun 12, 2004
Posts: 34
|
|
hello everybody, I have a small doubt regarding typecasting the string objects. what is the difference between converting an object by calling toString() method and by explicitly typecasting the object. I mean by explicitly giving (String) before the object. Thanks in advance
|
 |
Vijay Vaddem
Ranch Hand
Joined: Feb 13, 2004
Posts: 243
|
|
Hi, Please be more specific in your question.... If possible, post the code you are having trouble with.... Vijay
|
 |
David Ulicny
Ranch Hand
Joined: Aug 04, 2004
Posts: 724
|
|
toString method returns some String representation of an object. In your own classes you could override this method to return some useful information. Look at Thread. In general it returns some internal address of that reference. I'm not sure what you mean by explicitly typecasting. Maybe it is useful when you are working with collections.
|
SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
|
 |
Ramaswamy Srinivasan
Ranch Hand
Joined: Aug 31, 2004
Posts: 295
|
|
Hi Pavanasree, This is a good question in fact. It made me think. I am giving what my limited knowledge holds in store. The toString() method is found originally in the Object class. All the class, have the method. This is used when we need to print some useful, meaningful information about the object of the class. Otherwise, the System.out.println() returns a value as OurClassName@UnsignedHashCode of the Object. For example you have some primitive and if you need to read the primitive or an object for that matter, we override the toString() method. This will give a value that we can understand of our objects. And when it comes to casting....i welcome some ideas on the usage of these two methods. I have been using (String) explicitly to cast the result. Any luck guys? Cheers, Swamy
|
 |
satish sathineni
Ranch Hand
Joined: May 03, 2004
Posts: 46
|
|
Hi, It is really good question... .toString() is defined the Object class all other objects mostly extend from the Object class they overridden the .toString() This is done becoz to represent the meaningful representation of the class behaviour or stuff like that .... Now one important fact i came to know about String class and its .toString() is that it returns the value assigned to the String So when u r aware that the Return Type is String u can call either .toString() or (String) both gives u the same output the value of the String for other than String objects u cannot expect the output of the .toString() method Regards Satish
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
To be clear about what casting does: You cannot change the type of an object. You can cast a reference type in order to create a reference of a different type. This doesn't change the original reference, and it certainly has no effect on any object being referred to. As already pointed out, the only situation where ref.toString() == ref would be true, would be when ref refers to an object of type String. (ref could be of type String, Object, Serializable, Comparable or CharSequence.)
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
 |
|
|
subject: Type casting Strings
|
|
|