Both the execution of a switch statement (§14.11.3) and the evaluation of a switch expression (§15.28.2) need to determine if a switch label associated with the switch block applies to the value of the selector expression. This proceeds as follows:
-
If the value is the null reference, then a
caselabel with anullliteral applies. -
If the value is not the null reference, then we determine the first (if any)
caselabel in the switch block that applies to the value as follows:-
A
caselabel with acaseconstantcapplies to a value of typeCharacter,Byte,Short, orInteger, if the value is first subjected to unboxing conversion (§5.1.8) and the constantcis equal to the unboxed value.Any unboxing conversion will complete normally as the value being unboxed is guaranteed not to be the null reference.
Equality is defined in terms of the
==operator (§15.21). -
A
caselabel with acaseconstantcapplies to a value that is of typechar,byte,short,int, orStringor an enum type if the constantcis equal to the value.Equality is defined in terms of the
==operator unless the value is aString, in which case equality is defined in terms of theequalsmethod of classString. -
Determining that a
caselabel with acasepatternpapplies to a value proceeds first by checking if the value matches the patternp(§14.30.2).If pattern matching completes abruptly then the process of determining which switch label applies completes abruptly for the same reason.
If pattern matching succeeds and the
caselabel is unguarded then thiscaselabel applies.If pattern matching succeeds and the
caselabel is guarded, then the guard is evaluated. If the result is of typeBoolean, it is subjected to unboxing conversion (§5.1.8).If evaluation of the guard or the subsequent unboxing conversion (if any) completes abruptly for some reason, the process of determining which switch label applies completes abruptly for the same reason.
Otherwise, if the resulting value is
truethen thecaselabel applies. -
A
casenull,defaultlabel applies to every value.
-
-
If the value is not the null reference, and no
caselabel applies according to the rules of step 2, but there is adefaultlabel associated with the switch block, then thedefaultlabel applies.
A single case label can contain several case constants. The label applies to the value of the selector expression if any one of its constants is equal to the value of the selector expression. For example, in the following code, the case label applies if the enum variable day is either one of the enum constants shown:
switch (day) {
...
case SATURDAY, SUNDAY :
System.out.println("It's the weekend!");
break;
...
}
If a case label with a case pattern applies, then this is because the process of pattern matching the value against the pattern has succeeded (§14.30.2). If a value successfully matches a pattern then the process of pattern matching initializes any pattern variables declared by the pattern.
In C and C++ the body of a switch statement can be a statement and statements with case labels do not have to be immediately contained by that statement. Consider the simple loop:
for (i = 0; i < n; ++i) foo();
where n is known to be positive. A trick known as Duff's device can be used in C or C++ to unroll the loop, but this is not valid code in the Java programming language:
int q = (n+7)/8;
switch (n%8) {
case 0: do { foo(); // Great C hack, Tom,
case 7: foo(); // but it's not valid here.
case 6: foo();
case 5: foo();
case 4: foo();
case 3: foo();
case 2: foo();
case 1: foo();
} while (--q > 0);
}
Fortunately, this trick does not seem to be widely known or used. Moreover, it is less needed nowadays; this sort of code transformation is properly in the province of state-of-the-art optimizing compilers.