-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
6
-
x86
-
windows_xp
FULL PRODUCT 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 :
Microsoft Windows XP Media Center Edition [Version 5.1.2600] SP2
EXTRA RELEVANT SYSTEM CONFIGURATION :
AOpen MX36LE-UN, Intel Pentium 3 1GHz, SDRAM 1 DIMM 512 PC133
A DESCRIPTION OF THE PROBLEM :
(Un)Boxing does not behave according to JLS 3rd Ed. 5.1.7, 5.1.8:
"If the value p being boxed is true, false,a byte,a char in the range \u0000 to
\u007f, or an int or short number between -128 and 127, then let r1 and r2 be
the results of any two boxing conversions of p. It is always the case that r1==r2"
boolean TestAutoboxing_Boolean_WRONG() {
boolean x = true;
Boolean a = new Boolean(x);
Boolean b = new Boolean(x);
return a == b; // Compares by reference
}
boolean TestAutoboxing_Boolean_RIGHT() {
boolean x = true;
Boolean a = x;
Boolean b = x;
return a == b; // Compares by value
}
Both methods should return "true" proving that:
a.equals(b) == b.equals(a) == true
but just the second does even when, according to specs, in both cases the assignment to a and b are to be considered "boxings" with the only difference of "whom" is doing the boxing.
The same happens with all 8 types listed in the aformentioned JLS chapter.
Something similar was reported in Bug Id 6212662 but the report was closed stating it wasn't a bug.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just run the example method TestAutoboxing_Boolean_WRONG() and validate the return value.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When comparing two Boolean types the comparison shoul be always of the same type either by value or by reference but not both.
boolean test1 = TestAutoboxing_Boolean_RIGHT(); //TRUE
boolean test2 = TestAutoboxing_Boolean_WRONG(); //TRUE
boolean result = test1 & test2; //TRUE so is CORRECT
ACTUAL -
boolean test1 = TestAutoboxing_Boolean_RIGHT(); //TRUE
boolean test2 = TestAutoboxing_Boolean_WRONG(); //FALSE
boolean result = test1 & test2; //FALSE so is NOT CORRECT
According to JLS 3rd Ed. 5.1.7, 5.1.8 both methots should return TRUE and the first doesn't.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
boolean TestAutoboxing_Boolean_WRONG() {
boolean x = true;
Boolean a = new Boolean(x);
Boolean b = new Boolean(x);
return a == b; // Should return TRUE
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Avoid using unnecessary boxing. Some IDEs like Idea can actually detect this and suggest alternatives.
Use:
int x = 9;
Integer a = x;
Instead of:
int x = 9;
Integer a = new Integer(x);
If boxing can't be avoided consider using intemediate variables of native types.
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 :
Microsoft Windows XP Media Center Edition [Version 5.1.2600] SP2
EXTRA RELEVANT SYSTEM CONFIGURATION :
AOpen MX36LE-UN, Intel Pentium 3 1GHz, SDRAM 1 DIMM 512 PC133
A DESCRIPTION OF THE PROBLEM :
(Un)Boxing does not behave according to JLS 3rd Ed. 5.1.7, 5.1.8:
"If the value p being boxed is true, false,a byte,a char in the range \u0000 to
\u007f, or an int or short number between -128 and 127, then let r1 and r2 be
the results of any two boxing conversions of p. It is always the case that r1==r2"
boolean TestAutoboxing_Boolean_WRONG() {
boolean x = true;
Boolean a = new Boolean(x);
Boolean b = new Boolean(x);
return a == b; // Compares by reference
}
boolean TestAutoboxing_Boolean_RIGHT() {
boolean x = true;
Boolean a = x;
Boolean b = x;
return a == b; // Compares by value
}
Both methods should return "true" proving that:
a.equals(b) == b.equals(a) == true
but just the second does even when, according to specs, in both cases the assignment to a and b are to be considered "boxings" with the only difference of "whom" is doing the boxing.
The same happens with all 8 types listed in the aformentioned JLS chapter.
Something similar was reported in Bug Id 6212662 but the report was closed stating it wasn't a bug.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just run the example method TestAutoboxing_Boolean_WRONG() and validate the return value.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When comparing two Boolean types the comparison shoul be always of the same type either by value or by reference but not both.
boolean test1 = TestAutoboxing_Boolean_RIGHT(); //TRUE
boolean test2 = TestAutoboxing_Boolean_WRONG(); //TRUE
boolean result = test1 & test2; //TRUE so is CORRECT
ACTUAL -
boolean test1 = TestAutoboxing_Boolean_RIGHT(); //TRUE
boolean test2 = TestAutoboxing_Boolean_WRONG(); //FALSE
boolean result = test1 & test2; //FALSE so is NOT CORRECT
According to JLS 3rd Ed. 5.1.7, 5.1.8 both methots should return TRUE and the first doesn't.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
boolean TestAutoboxing_Boolean_WRONG() {
boolean x = true;
Boolean a = new Boolean(x);
Boolean b = new Boolean(x);
return a == b; // Should return TRUE
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Avoid using unnecessary boxing. Some IDEs like Idea can actually detect this and suggest alternatives.
Use:
int x = 9;
Integer a = x;
Instead of:
int x = 9;
Integer a = new Integer(x);
If boxing can't be avoided consider using intemediate variables of native types.
- relates to
-
JDK-6212662 Boxing/Unboxing detector for == that will always fail
-
- Closed
-