• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Is System.out.println method calling toString() by default? A strange behavior.

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought println() method would implicitly invoke toString() on an object when not explicitly stated. But here is an "inconsistent" behavior that I cannot explain to myself. Anybody has any idea what is happening here and why?

Case 1. Using toString() implicitly(?)

Output:

Case 2. Using toString() explicitly.

Output:
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't really inconsistent. In Case 1, you're passing a valid object reference to println(), so println() will call that object's toString() method and expect it to return a String object. In Case 2, you're passing a null reference to println(), so println() simply prints "null" without ever trying to call a toString() method.

edit: I forgot to mention that you're also invoking two different overloaded versions of println() here. Case 1 invokes println(Object), whereas Case 2 invokes println(String), due to the "most specific method" disambiguation mechanism.
[ December 06, 2007: Message edited by: Kelvin Lim ]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point Kevin. I just did a javap -c to look at the bytecode and sure enough you can see that different methods are being called:


[ December 06, 2007: Message edited by: nico dotti ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, System.out.println() does not call toString(). It calls String.valueOf(Object). The API doc of String.valueOf(Object) says:

Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can you pleae little elaborate on this.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't understand this behaviour



Output is: NullPointerException



Output is: null
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andry Dub:
I can't understand this behaviour



It's the same thing as the original example. If you pass "null" to println() (and thus to String.valueOf()), it is properly handled. String.valueOf(null) returns "null", as a special case.

If, on the other hand, you pass an Object whose toString() method returns null, then valueOf() calls toString() on that object, gets a null value that it doesn't expect, and throws an NPE.

So to summarize: valueOf() can handle a null argument, but it can't handle an object that returns null from toString(). Writing toString() that returns null is an evil thing to do, in any case.
 
Andry Dub
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest Friedman-Hill, thank you very much for explanations.
 
Marcus Jastrebowski
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, I am a quite confused, since K&B book states that

"... toString() method is automatically called when you ask System.out.println() to print an object..."

(Chapter 7, page 605, Certification Summary).

So perhaps I'm missing some key point here, but in this exchange, Jesper and Ernest, you both seem to be saying that the statement above is not quite right? Would somebody please explain what exactly happens when System.out.println() method is called to print an object?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So perhaps I'm missing some key point here, but in this exchange, Jesper and Ernest, you both seem to be saying that the statement above is not quite right? Would somebody please explain what exactly happens when System.out.println() method is called to print an object?



I think that you should reread the exchange again. Neither Jesper or Ernest said that the toString() method is not called -- they just said that it is not called directly. (going through the valueOf() method)

Henry
 
Marcus Jastrebowski
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. That was the point I was missing.
reply
    Bookmark Topic Watch Topic
  • New Topic