Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.out.print problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KB book 543

when an object reference is passed to System.out.print,it
gives the class name (at least that's meaningful)
followed by the @ symbol, followed by the unsigned hexadecimal representation of
the object's hashcode.


but when an object of type string or Integer or List or Set or any of the collections is passed,it gives the content of that type



how this possible ?
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.print() calls toString() on the object you pass it. Each of the classes that you mention overrides toString() to provide output that is more user friendly.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
list ,sets, maps too override toString() ?
are there any more classes which override toString() ?
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:list ,sets, maps too override toString() ?
are there any more classes which override toString() ?

Most API classes do override where it makes sense.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO most of the final classes overrides toString() method.

One of the final class which does not overrides toString() i know is java.util.Arrays class
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic