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

toString()

 
Kirthi Ragavan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9320
10
Linux Mac OS X Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic