This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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: 10744

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.
Have you checked out Aspose?
subject: Need help in understanding the basics
It's not a secret anymore!