aspose file tools*
The moose likes Beginning Java and the fly likes unique identifier of an instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "unique identifier of an instance" Watch "unique identifier of an instance" New topic
Author

unique identifier of an instance

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Is it secure to use the class.toString method for identifying an object. For example, when using instance.toString I get this:

com.test.User@b9d1cf3f

So I can use the string "com.test.User@b9d1cf3f" as an unique identifier of this instance. Am I right?
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

It shall be secure only for classes that do not override the toString() method of the Object class. If some class overrides, then there is high probability that you might identify 2 different instances for the same instance.
Use hashCode() instead....or even more simpler check with '=='.


OCPJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38392
    
  23
nimo frey wrote:Is it secure to use the class.toString method for identifying an object.
No
Am I right?
No. You can try using the System#identityHashCode() method, but I don't think that will definitely identify an object apart from its identifier. The details of hashCode can vary from implementation to implementation, so that technique might give different results on different implementations.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38392
    
  23
Vinoth Kumar Kannan wrote: . . . Use hashCode() instead . . .
Two different instances which return the same String from toString() will probably also return true from equals(). In which case they will return the same hash code.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Campbell Ritchie wrote:
Vinoth Kumar Kannan wrote: . . . Use hashCode() instead . . .
Two different instances which return the same String from toString() will probably also return true from equals(). In which case they will return the same hash code.

Ohhh..... yes, I missed that. I must have said System.identityHashCode() instead.
Thanks for correcting, Campbell.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Hello both,

thank you !

I need the unique id of an instance for a JSF-Converter (to convert between string and object-instance). When getting the string back from front-end to back-end, then it is something like this: ''com.test.User@b9d1cf3f''. So I have thought that jsf internally stores the reference of instances as this string and not its hashcode. I have found out, that this string with "@b9d1cf3f" identifies my instance successfully and my converter works. But maybe I mixed up equals and identity..

However, I will use System.identityHashCode() instead of Object.hashCode() or ''com.test.User@b9d1cf3f''.

thanks!
 
wood burning stoves
 
subject: unique identifier of an instance