This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP 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