*
The moose likes Java in General and the fly likes Difference between Object.toString() and String s= (String)Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between Object.toString() and String s= (String)Object" Watch "Difference between Object.toString() and String s= (String)Object" New topic
Author

Difference between Object.toString() and String s= (String)Object

Praveen Kumar KR
Greenhorn

Joined: May 08, 2002
Posts: 8
Hi All,
I would like to know what is the diffrrence between using Object.toString() method and type casting like String s = (String)Object.
What should be used when ?
Thanks in advance.
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Just try:
Object toto = new Object();
String s =(String)toto;
Result:
java.lang.ClassCastException: IntDoCheckCast
at test/Bobby.<init>(Bobby.java:20)
at test/Bobby.main(Bobby.java:26)
An Object is not a String!
W.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
There was another thread on this subject last month. Check it out here


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
 
 
subject: Difference between Object.toString() and String s= (String)Object
 
Similar Threads
assertion expression from K&B
why new date() prints date ?????
Why Compiler error?.....
Using a char array in string concatenation
Difference between valueOf()Method and toString()Method