• Post Reply Bookmark Topic Watch Topic
  • New Topic

enum hash code calculation are not the same  RSS feed

 
Deniz Kilic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an update page of users of a website. Some columns of user were coded in an enum. All the information of user goes to a POJO and all the columns were hashed. My problem is, when I open this update page and update some information of any user, It Works successfully. However, when I open this web page in another computer, the update process does not work clearly. I looked at the hash codes of columns by using 'system.out.println' and I saw two different computers produced two different hash codes in columns which are done with the enum base. I hope that my problem is clear. Thanks for any reply.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deniz Kilic wrote:I saw two different computers produced two different hash codes in columns which are done with the enum base. I hope that my problem is clear. Thanks for any reply.

That's within the contract of hashCode.
From the Object.hashCode javadoc
This integer need not remain consistent from one execution of an application to another execution of the same application.
 
Deniz Kilic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But some of colums had the same hash result ,only some of them which were coded with enum base had different hash result.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on how the hashcode is generated. It's quite possible that different executions of a program could produce the same hashcode for some objects and different ones for others.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.Enum's hashCode() implementation directly returns the result of super.hashCode(), which is java.lang.Object's implementation. That implementation is documented as follows:

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 Java programming language).

So yes, different systems or even JVM instances on the same system will most likely generate different hashCode() results for the same enum type.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!