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

Does toString() return address of the object?

 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java says toString() returns "Name of the class"+"@"+"the unsigned hexadecimal representation of the hash code of the object". Does "the unsigned hexadecimal representation of the hash code of the object" represent the address of the object?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API documentation of java.lang.Object.hashCode() says:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

In other words, it might be, but there's no guarantee that it's the internal address of the object.
 
Henry Wong
author
Marshal
Pie
Posts: 20991
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Another point. Hash codes don't change -- as they may be used in hashing collections. On the other hand, objects get moved. And with certain GC algorithms, such as copy collectors, they get moved every time. So, if there is a relationship between the memory address and the hashcode, it doesn't stay that way for very long.

Henry

 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Jesper..
this implementation technique is not required by the JavaTM programming language


In other words, it might be, but
there's no guarantee that it's the
internal address of the object


Any takers? I'll appreciate further information.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another point. Hash codes don't change -- as they may be used in
hashing collections. On the other hand, objects get moved. And with certain GC algorithms, such as copy collectors, they get moved every time. So, if there is a relationship between the memory address and the hashcode, it doesn't stay that way for very long.


Thanks! Henry for mentioning this point. Are you indirectly prompting that 'the string' doesn't refer to the address of concerned object?
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real point is this: It doesn't make any difference at all whether the hashcode is the address of the object or not. And you shouldn't care whether it is, either. All you should care about is whether it satisfies the contract which a hashcode should satisfy.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might look like an address, but it is not. You can control both the toString and the hashCode functions, whereas you can not even access the address of the objects, let alone directly influence or control it.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Jesper, Henry, Paul and Ivan for your reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic