This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How this print() method works? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How this print() method works?" Watch "How this print() method works?" New topic

How this print() method works?

Debapriya Mukherjee

Joined: Mar 07, 2012
Posts: 4
When I am Trying to Print an Object

It gives me
This seems to be a output of toString() method.
How this internal call happening?
I tried seeing the class hierarchy but getting confused?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

This is how you find out:

- Look at the API documentation of class java.lang.System
- You see that it has a static member variable called "out", which is a
- Look at the API documentation of class
- You see that it has several overloaded versions of the print(...) method
- For your A object, it uses the print(Object) version
- Look at the API documentation for that method; it says:
API documentation wrote: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.

So now you know that it calls String.valueOf(...) with your object as the argument. The API documentation of String.valueOf(...) says:
API documentation wrote:Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

In other words, this calls toString() on your object.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37958
Read about toString() here.
wood burning stoves
subject: How this print() method works?
Similar Threads
print in JSP method
Printing number of records from a ResultSet
Printing Question
Print something in java.
how does java implement all this ?