• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.out.println(this)?

 
Pui Shing Lee
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I compile and run, the output is:
In test
trial42@107077e
10


My question is what does "System.out.println(this);" do? Why it give trial42@107077e?
[ July 25, 2005: Message edited by: Barry Gaunt ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
println(Object) calls toString() on the argument; this String is then displayed. See the Javadocs for java.lang.Object.toString() (which your class inherits, because it doesn't override this method) for a detailed explanation.
 
Mohamed Shafi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Pui Shing Lee,

this usually invokes the current reference object.so when u try to print using the line

System.out.println(this);

u r trying to print the object reference using toString(). check out the sample ...

class trial
{
public static void main(String args[])
{
trial obj=new trial();
System.out.println(obj);
}
}

u get output as trial@13e8d89

Hope this helps...


Regards,

Shafi.
 
lalit upadheyay
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does "System.out.println(this);" do?

It implicitly invokes the toString() method for anything supplied to it as parameter ( whether primitive or object references ), here the current object reference . Since your class does not provide implementation of public String toString(Object o) method , it will inherit implemenattion from its parent class i.e. Object class, and will invoke it here.

Why it give trial42@107077e?
If your class does not override the implementation of toString() method inherited by your class from the Object class ( grand dady of all classes ) then you will get class name here trial42 followed by hashCode() for the object. For details regarding these methods i would suggest you to refer to API documentation for Object class
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic