• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How this print() method works?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I am Trying to Print an Object
Suppose,

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

Regards,
Deb
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 java.io.PrintStream
- Look at the API documentation of class java.io.PrintStream
- 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read about toString() here.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic