What I knew up to now about switch/case construct: 1�) the variable x in switch(x) must be either byte, short, char or int. It must not be long, either of the floating-point types, boolean, or an object reference. 2�) The arguments to case labels must be compile constant expression according to JLS 15.28:
# Simple names that refer to final variables whose initializers are constant expressions # Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions
3�) What I've just learned from Bill Brogden's Questions is that the type used in the switch statement must accommodate all of the values in the case statements Thus, this will not compile:
because of lines 2 and 3 in which range > 127. But this wil compile with success:
Maybe it's worth knowing this... Cyril.
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
That question is the most frequently missed one in my "Hardest" collection. I think the most important thing it points out is - don't jump to conclusions as to what a question is about. I suspect people read the question and jump to the conclusion that it is about the requirement for a variable that can be promoted to int in the switch statement. Then they look at the options and choose everything but long and charge on to the next question. Bill