File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Need help in understanding the basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help in understanding the basics" Watch "Need help in understanding the basics" New topic

Need help in understanding the basics

R Babu

Joined: Oct 14, 2008
Posts: 9
I want to understand how the code given here works. Specifically the last line with the println method. I'm just passinig the object without calling the toString() method on that object. Yet I'm getting the desired output. Shouldn't it print some address location ? Can someone please clarify me?

Mark Spencer

Joined: Feb 02, 2008
Posts: 8
If we see the API, print object calls String.valueOf(object) which if object is null returns string "null" else object.toString().

Correct me if I am wrong.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268

You are absolutely correct. This is the actual code

@ "R. Babu,"
From the API documentation
Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

As you can see, when you call the "println" method and pass the object reference, it internally calls the String.valueOf() for that particular object.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
R Babu

Joined: Oct 14, 2008
Posts: 9
Thanks Mark and Maneesh. It makes a lot of sense.
It is sorta covered in the JavaRanch Style Guide.
subject: Need help in understanding the basics