This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 '=='.
nimo frey wrote:Is it secure to use the class.toString method for identifying an object.
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.
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.
Joined: Jun 28, 2008
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''.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com