GeeCON Prague 2014*
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
Author

Need help in understanding the basics

R Babu
Greenhorn

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
Greenhorn

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: 10426
    
    8

Mark,
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
Greenhorn

Joined: Oct 14, 2008
Posts: 9
Thanks Mark and Maneesh. It makes a lot of sense.
 
GeeCON Prague 2014
 
subject: Need help in understanding the basics