• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unique identifier of an instance

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 '=='.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49379
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49379
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic