Monday 15 April 2013

object identityhashcode in java -


I have this question.

I am working in my project (I also use emf) I need to keep a unique ID for each object that I am using, so I used to identify HashCode Decided, as far as I understand, this value is similar to compilation.

I have given the object as a parameter in the law of another class, but when I try to get the hashod, this is not what I print the value of the object.

Something like this:

System.out.println ("Object is:" + obj)

System. Out.println (hash id is: + Integer.toHexString (System.identityHashCode (obj));

But as a result I get this:

is an object : *** Xxxxxxxxxxxxxx***.EntityImpl@18e588c (name: comment) has been deleted.

hash id is: 1ec1758 < / P>

As you can see that two values ​​are completely different, but I do not understand that the only thing that I have (and this works) is the st To get the ring and then 18e588c (for this example)

I appreciate any answer.

Regards

I need to keep a unique ID to use every object, so I use the identity of HashCode I have decided, as far as I think this value is similar to compilation.

No, it has nothing to do with compilation, and it no Not is guaranteed to be unique.

It is not clear what you are trying to do, but you are unique in the form of hash codes - they are not guaranteed to happen.

Object.hashCode specifies the document:

equally practical enough,

Although it is not the same thing as a guarantee.

due to being confused with the result of calling toString () - I suspect that your class actually overrides hash code ( ) , and Object.toString () instead of using the hash code, instead of potentially override hashhead () method calls: < Blockquote>

Returns the string of the toString method for the class object, the object of the range whose object is an example, the sign-on character '@', and the trailing de hexadecimal object Represent the hash code of T. In other words, this method returns a string equal to one:

  getClass (). GetName () + '@' + Integer. HostStress (hash code ())   obj.hashCode () , you will see the same value as  ToString .   

No comments:

Post a Comment