Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Marcus Jastrebowski
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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:
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • 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 ]
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can you pleae little elaborate on this.
 
Andry Dub
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand this behaviour



Output is: NullPointerException



Output is: null
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you. That was the point I was missing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic