FULL PRODUCT VERSION :
1.6.02
java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) Server VM (build 1.5.0_07-b03, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Windows XP SP2
Ubuntu 6.04
SunOS 5.10 Generic_118833-24 sun4v sparc SUNW,Sun-Fire-T200
A DESCRIPTION OF THE PROBLEM :
if the switch statement is changes to be an "Integer" wrapper type, the non-int case statements fail to compile. if the switch statement is of type "Character", "Byte" or "Short" all works fine. I posted this problem on different websites. I got no answer. people guess that it is a bug. i am playing around for the SCJP exam, so the weird combination in this switch test is just for "playing". the results are not as I would expect them to be.
when all the wrappers except of "Integer" work (it works when changed to primitve "int") it seems like a bug to me. e.g. "Short" should also not be assignable to "Byte". So how _exactly_ are the steps the compiler takes to resolve the problem. Other compilers e.g. Eclispe internal one says all cases are ok. just the Sun compiler doesn't think it is ok!
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
try the code
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
when all the wrappers except of "Integer" work (it works when changed to primitve "int") it seems like a bug to me.
ACTUAL -
compile failed for non-int switch "cases"
ERROR MESSAGES/STACK TRACES THAT OCCUR :
SwitchTest.java:21: incompatible types
found : short
required: java.lang.Integer
case sP: System.out.println("");
^
SwitchTest.java:22: incompatible types
found : char
required: java.lang.Integer
case 'n': System.out.println("");
^
SwitchTest.java:23: incompatible types
found : byte
required: java.lang.Integer
case bP: System.out.println("");
^
3 errors
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class SwitchTest
{
public static void main(String[] args)
{
final int iP = 1;
final short sP = 126;
final byte bP = 2;
Short sLw = 3;
Byte bLw = 4;
Character cLw = 5;
Integer iLw = 6;
// switch (cLw){ // works fine, just uncomment it
// switch (bLw){ // works fine, just uncomment it
// switch (sLw){ // works fine, just uncomment it
switch (iLw) { // the non-int cases FAIL - WHY?
case sP:
System.out.println("");
case 'n':
System.out.println("");
case bP:
System.out.println("");
case iP:
System.out.println("");
case 127:
System.out.println("");
}
}
}
---------- END SOURCE ----------