The switch statement transfers control to one of several statements depending on the value of an expression.
SwitchStatement:
switch ( Expression ) SwitchBlock
SwitchBlock:
{ {SwitchBlockStatementGroup} {SwitchLabel} }
SwitchBlockStatementGroup:
SwitchLabels BlockStatements
SwitchLabels:
SwitchLabel {SwitchLabel}
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
EnumConstantName:
Identifier
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.
The body of a switch statement is known as a switch block. Any statement immediately contained by the switch block may be labeled with one or more switch labels, which are case or default labels. Every case label has a case constant, which is either a constant expression or the name of an enum constant. Switch labels and their case constants are said to be associated with the switch statement.
Given a switch statement, all of the following must be true or a compile-time error occurs:
- Every
caseconstant associated with theswitchstatement must be assignment compatible with the type of theswitchstatement's Expression (§5.2). - If the type of the
switchstatement's Expression is an enum type, then everycaseconstant associated with theswitchstatement must be an enum constant of that type. - No two of the
caseconstants associated with theswitchstatement have the same value. - No
caseconstant associated with theswitchstatement isnull. - At most one
defaultlabel is associated with theswitchstatement.
The prohibition against using null as a case constant prevents code being written that can never be executed. If the switch statement's Expression is of a reference type, that is, String or a boxed primitive type or an enum type, then an exception will be thrown will occur if the Expression evaluates to null at run time. In the judgment of the designers of the Java programming language, this is a better outcome than silently skipping the entire switch statement or choosing to execute the statements (if any) after the default label (if any).
A Java compiler is encouraged (but not required) to provide a warning if a switch on an enum-valued expression lacks a default label and lacks case labels for one or more of the enum's constants. Such a switch will silently do nothing if the expression evaluates to one of the missing constants.
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.
A switch statement is executed by first evaluating the Expression. If the Expression evaluates to null, a NullPointerException is thrown and the entire switch statement completes abruptly for that reason. Otherwise, if the result is of type Character, Byte, Short, or Integer, it is subject to unboxing conversion (§5.1.8).
If evaluation of the Expression or the subsequent unboxing conversion (if any) completes abruptly for some reason, the switch statement completes abruptly for the same reason.
Otherwise, execution continues by comparing the value of the Expression with each case constant, and there is a choice:
-
If one of the
caseconstants is equal to the value of the expression, then we say that thecaselabel matches. Equality is defined in terms of the==operator (§15.21) unless the value of the expression is aString, in which case equality is defined in terms of theequalsmethod of classString.All statements after the matching
caselabel in theswitchblock, if any, are executed in sequence.If all these statements complete normally, or if there are no statements after the matching
caselabel, then the entireswitchstatement completes normally. -
If no
caselabel matches but there is adefaultlabel, then all statements after thedefaultlabel in theswitchblock, if any, are executed in sequence.If all these statements complete normally, or if there are no statements after the
defaultlabel, then the entireswitchstatement completes normally. -
If no
caselabel matches and there is nodefaultlabel, then no further action is taken and theswitchstatement completes normally.
If any statement immediately contained by the Block body of the switch statement completes abruptly, it is handled as follows:
-
If execution of the Statement completes abruptly because of a
breakwith no label, no further action is taken and theswitchstatement completes normally. -
If execution of the Statement completes abruptly for any other reason, the
switchstatement completes abruptly for the same reason.The case of abrupt completion because of a
breakwith a label is handled by the general rule for labeled statements (§14.7).
Example 14.11-1. Fall-Through in the switch Statement
As in C and C++, execution of statements in a switch block "falls through labels."
For example, the program:
class TooMany {
static void howMany(int k) {
switch (k) {
case 1: System.out.print("one ");
case 2: System.out.print("too ");
case 3: System.out.println("many");
}
}
public static void main(String[] args) {
howMany(3);
howMany(2);
howMany(1);
}
}
contains a switch block in which the code for each case falls through into the code for the next case. As a result, the program prints:
many
too many
one too many
If code is not to fall through case to case in this manner, then break statements should be used, as in this example:
class TwoMany {
static void howMany(int k) {
switch (k) {
case 1: System.out.println("one");
break; // exit the switch
case 2: System.out.println("two");
break; // exit the switch
case 3: System.out.println("many");
break; // not needed, but good style
}
}
public static void main(String[] args) {
howMany(1);
howMany(2);
howMany(3);
}
}
This program prints:
one
two
many