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

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?

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.

"... 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?

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)