File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
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: 45
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

Joined: Aug 16, 2005
Posts: 14272
    
  21

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 8 API documentation
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


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: 45
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: 45
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: 18669
    
    8

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

Joined: May 22, 2012
Posts: 867
    
    5
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: 45
Thanks! Jesper, Henry, Paul and Ivan for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does toString() return address of the object?