JDK-7190798 : java enum hashcode not stable among JDK 7
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2012-08-11
  • Updated: 2022-08-10
  • Resolved: 2012-08-13
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
JDK 1.7 Version
=============
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b22)
Java HotSpot(TM) Client VM (build 23.0-b21, mixed mode, sharing)

JDK 1.6 version
=============
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)



ADDITIONAL OS VERSION INFORMATION :
System: Microsoft XP Professional version 2002 Service Pack 3
Version: 5.1.2600 Service Pack 3 Build 2600

A DESCRIPTION OF THE PROBLEM :
The hash code value is not stable in JDK 1.7, but t is same in JDK 1.6 if you run multiple time.

REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Source code is attached. Kindly run the source code for the JDK version mentioned.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The hash code value is not stable in JDK 1.7
ACTUAL -
JDK 1.7
=======
G:\javapgms>java TestEnum
hashCode get: 18426253
hashCode instance: 77

G:\javapgms>java TestEnum
hashCode get: 3195425
hashCode instance: 77

G:\javapgms>java TestEnum
hashCode get: 3195425
hashCode instance: 77

JDK 1.6
======
G:\javapgms>java TestEnum
hashCode get: 11394033
hashCode instance: 77

G:\javapgms>java TestEnum
hashCode get: 11394033
hashCode instance: 77

G:\javapgms>java TestEnum
hashCode get: 11394033
hashCode instance: 77

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
The below program were tried in JDK 1.6 and JDK1.7

The hash code value is not stable in JDK 1.7, but it is same in JDK 1.6

class TestEnum {
    enum Gender {
        M("Male"), F("Female");
		
		private String description;
		
		Gender(String description){
			this.description = description;
		}
    }

	public static void main(String args[]) {
		TestEnum t = new TestEnum();
		t.setGender(Gender.M.name());
		System.out.println("hashCode get: " + t.getGender().hashCode());
		System.out.println("hashCode instance: " + t.gender.hashCode());
    }
	
	public String gender = null;
	
	public void setGender(String gender){
		this.gender = gender;
	}
	
	public Gender getGender(){
		return Gender.valueOf(this.gender);
	}
	
}


---------- END SOURCE ----------

Comments
PUBLIC COMMENTS As per the API docs for hashCode(), the general contract is as follows: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. Printing the value of t.getGender().hashCode() multiple times in the same execution reveals the same value which adheres to the contract above. Value of t.getGender().hashCode() can change everytime the application is run, so closing this bug as not a defect.
13-08-2012