Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper class doubt

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i=new Integer(100);
System.out.println(i); // why is this line printing 100 and not the reference address
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the Integer class has its toString() method overridden.

And you never see the reference address of objects. What you see when you print an Object instance is its hashCode. It is most likely based on the object address in memory, but it doesn't have to. It most certainly never is the reference address - that's completely hidden. Remember, references aren't pointers like in C.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic