• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

toString( ) method ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the significance of toString() method of Object class .

both are same ...
can any body ...
thanks .
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(c) implicitly calls the toString method in c, so this is the same as System.out.println(c.toString()).
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite sure what Marc means by "implicitly", but PrintStream has two overloaded versions of println -- one takes an Object (c), and one takes a String (c.toString() -- which returns a String of course). So really, it's nothing special. It's just that the implementation of PrintStream.println(Object) calls toString on the passed Object.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nathaniel Stoddard:
Not quite sure what Marc means by "implicitly"...


Yes, perhaps "implicitly" was a poor word choice. I was just trying to say that the toString method is called behind the scenes.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The Object Class ie the supermost class defines a mthod toString() which returns
'the name of the class to which the object belongs to '+'@'+"hexadecimal hashcode of the object'.

This method may be overridden in other classes as per requirements.
Like it is overriden in String class to return the String value.....
Regards
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a big difference between

and


Please, if possible, use the first one.
Because, if you had following code



You will obtain a NullPointerException for Line 2, but Line 1 will display null on the console.

Vincent
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...

For PrintStream's print(Object) method, the API says that the "string producted by the String.valueOf(Object) method is translated into bytes ... [then] written in exactly the manner of the write(int) method."

And the return for String.valueOf(Object) is described as follows: "if the argument is null, then a string equal to 'null'; otherwise, the value of obj.toString() is returned."

So the difference appears to be in that intermediate call to String.valueOf(Object) being able to handle a null reference, which I did not realize.

Thanks for pointing this out.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic