Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in understanding the basics

 
R Babu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R Babu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark and Maneesh. It makes a lot of sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic