-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
6
-
Fix Understood
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.6.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta2-b73)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b73, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
The JLS 3 only allow to swicth on
char, byte, short, int, Character, Byte, Short, Integer, or an enum type (see section 14.11)
and not on type assignable without canversion in
char, byte, short, int, Character, Byte, Short, Integer, or an enum type.
With variance wilcard introduced in JDK 1.5), you can now create
subtype of final type but you can't switch on it, the code below don't
compile according to the spec.
List<? extends Integer> list=Arrays.asList(1,2);
switch(list.get(0)) {
case 1:
case 2:
}
I think the spec could be changed to let this code compile.
Rémi Forax
REPRODUCIBILITY :
This bug can be reproduced always.
CUSTOMER SUBMITTED WORKAROUND :
use a temporary variable :
List<? extends Integer> list=Arrays.asList(1,2);
int value=list.get(0);
switch() {
case 1:
case 2:
}
java version "1.6.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta2-b73)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b73, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
The JLS 3 only allow to swicth on
char, byte, short, int, Character, Byte, Short, Integer, or an enum type (see section 14.11)
and not on type assignable without canversion in
char, byte, short, int, Character, Byte, Short, Integer, or an enum type.
With variance wilcard introduced in JDK 1.5), you can now create
subtype of final type but you can't switch on it, the code below don't
compile according to the spec.
List<? extends Integer> list=Arrays.asList(1,2);
switch(list.get(0)) {
case 1:
case 2:
}
I think the spec could be changed to let this code compile.
Rémi Forax
REPRODUCIBILITY :
This bug can be reproduced always.
CUSTOMER SUBMITTED WORKAROUND :
use a temporary variable :
List<? extends Integer> list=Arrays.asList(1,2);
int value=list.get(0);
switch() {
case 1:
case 2:
}
- relates to
-
JDK-8054172 14.11: switch runtime behavior doesn't account for reference types
-
- Closed
-
-
JDK-5012262 Using Strings and Objects in Switch case statements.
-
- Closed
-
-
JDK-6721146 type-inference from return-type not applied in conditional expressions
-
- Closed
-