wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString()" Watch "toString()" New topic
Author

toString()

Kirthi Ragavan
Greenhorn

Joined: Jan 08, 2013
Posts: 3
Hai,
When we print the object of the class like System.out.print(new Class()) default constructor of that class
will be called.. And toString() method also will be called.. Can anyone explain me what s reason behind tat??
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8935
    
    9

It's that way by design:

public void print(Object obj)
Prints an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

PrintWriter.print()
How else would you convert an Object to a printable representation?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
Consider Paul's rocket mass heater.
 
subject: toString()