These posts are confusing variables and Objects. Remember that variables are either primitives (ints etc, not Objects) or reference variables (contain a reference to an Object); a variable is NOT an Object.
Object's toString() is not defined to return anything related to the address of the object, nor does it return anything related to a reference. The API doc is perfectly clear - it returns the class name and the objects hash code.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
The hashCode() methodmay return something relating to the address, but it's not guaranteed
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.)
Having said all that, there is nothing in the Java language that corresponds to a memory address, and there is no syntax for accessing memory addresses.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
If we use toString() method to print the object ID then it is only print the reference ID not the object ID.
This thread is from 2008. I'm posting to it just to correct errors in the replies in case anyone searches for this topic.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073