Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Praveen Kumar KR
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was another thread on this subject last month. Check it out here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic