File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is System.out.println method calling toString() by default?  A strange behavior. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is System.out.println method calling toString() by default?  A strange behavior." Watch "Is System.out.println method calling toString() by default?  A strange behavior." New topic
Author

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

Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
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

Joined: Oct 20, 2007
Posts: 513
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 ]

SCJP 5.0
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
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

Joined: Aug 16, 2005
Posts: 13872
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello,

Can you pleae little elaborate on this.


Thanks<br />Dinesh
Andry Dub
Ranch Hand

Joined: Nov 06, 2007
Posts: 43
I can't understand this behaviour



Output is: NullPointerException



Output is: null


Java is my love
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Andry Dub
Ranch Hand

Joined: Nov 06, 2007
Posts: 43
Ernest Friedman-Hill, thank you very much for explanations.
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
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
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Thank you. That was the point I was missing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is System.out.println method calling toString() by default? A strange behavior.
 
Similar Threads
Reference to objects
Null Pointer Exception in Java
q from jiris.com
when toString() is implicitly called
Mock exam question