This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Does toString() return address of the object? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Does toString() return address of the object?" Watch "Does toString() return address of the object?" New topic
Author

Does toString() return address of the object?

Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 41
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
Bartender

Joined: Aug 16, 2005
Posts: 12928
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 41
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

Joined: Dec 11, 2012
Posts: 41
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
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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
Ranch Hand

Joined: May 22, 2012
Posts: 380
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

Joined: Dec 11, 2012
Posts: 41
Thanks! Jesper, Henry, Paul and Ivan for your reply.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Does toString() return address of the object?
 
Similar Threads
InetAddress() not working in Linux
usage of "this"
Please help with NullPointerException
compile time polymorphism?
Understanding creating a "super" class obj from a "subclass" object