JDK-7040854 : Different Integer objects created for the same primitive int value
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 6u24
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2011-04-30
  • Updated: 2012-03-20
  • Resolved: 2011-05-20
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing)



ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.



EXTRA RELEVANT SYSTEM CONFIGURATION :
TextPad

A DESCRIPTION OF THE PROBLEM :
"However, a Java implementation may, if it chooses, wrap commonly occurring values
into identical objects, and thus the comparison might succeed. This ambiguity is not
what you want. The remedy is to call the equals method when comparing wrapper objects.

java.lang.Integer a = 1000;
javal.lang.Integer b = 1000;"

Amazon Core Java Volume I (8th Edition)
http://www.amazon.com/Core-Java-TM-I-Fundamentals-8th/dp/0132354764, Pg. 212.

This statement could not be confirmed, however, I do not have any knowledge of Java internals.



REGRESSION.  Last worked in version 1.4.2


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.lang.*;

public class TestEight
{
	public static void main (String[] args)
	{
		Integer one = 1;
		Integer two = 1;
		if (one == two) System.out.println("one == two");
		if (one.equals(two)) System.out.println("one.equals(two)");

	}
}
---------- END SOURCE ----------

Comments
EVALUATION The JVM is not required to perform the optimization that Integer objects for the same primitive value be the same object
20-05-2011

PUBLIC COMMENTS Per the description the JVM is not required to perform the optimization that Integer objects for the same primitive value be the same object. If having the performance of == is critical to your application then you should provide your own interning hash map for Integer values.
20-05-2011