A switch expression transfers control to one of several statements or expressions, depending on the value of an expression; all possible values of that expression must be handled, and all of the several statements and expressions must produce a value for the result of the switch expression.
SwitchExpression:
switch ( Expression ) SwitchBlock
The Expression is called the selector expression. The type of the selector expression must be char, byte, short, int, or a reference type, or a compile-time error occurs.
The body of both a switch expression and a switch statement (§14.11) is called a switch block. General rules which apply to all switch blocks, whether they appear in switch expressions or switch statements, are given in §14.11.1. The following productions from §14.11.1 are shown here for convenience:
SwitchBlock:
{ SwitchRule {SwitchRule} }
{ {SwitchBlockStatementGroup} {SwitchLabel :} }
SwitchRule:
SwitchLabel -> Expression ;
SwitchLabel -> Block
SwitchLabel -> ThrowStatement
SwitchBlockStatementGroup:
SwitchLabel : {SwitchLabel :} BlockStatements
SwitchLabel:
case CaseConstant {, CaseConstant}
case null [, default]
case CasePattern {, CasePattern} [Guard]
default
CaseConstant:
ConditionalExpression
CasePattern:
Pattern
Guard:
when Expression
15.28.1. The Switch Block of a switch Expression
In addition to the general rules for switch blocks (§14.11.1), there are further rules for switch blocks in switch expressions.
It is a compile-time error if the switch block of a switch expression consists of switch rules, but one or more switch rule blocks can complete normally (§14.22).
It is a compile-time error if the switch block of a switch expression consists of switch labeled statement groups, but the last statement in the switch block can complete normally, or the switch block has one or more switch labels after the last switch labeled statement group.
It is a compile-time error if a switch expression is not exhaustive (§14.11.1.1).
switch expressions differ from switch statements in terms of which expressions may appear to the right of an arrow (->) in the switch block, that is, which expressions may be used as switch rule expressions. In a switch expression, any expression may be used as a switch rule expression, but in a switch statement, only a statement expression may be used (§14.11.1).
The result expressions of a switch expression are determined as follows:
-
If the switch block consists of switch rules, then each switch rule is considered in turn:
- If the switch rule is of the form
...->Expression;then Expression is a result expression of theswitchexpression. - If the switch rule is of the form
...->Block then every expression which is immediately contained in ayieldstatement in Block whose yield target is the givenswitchexpression, is a result expression of theswitchexpression.
- If the switch rule is of the form
-
If the switch block consists of switch labeled statement groups, then every expression immediately contained in a
yieldstatement in the switch block whose yield target is the givenswitchexpression, is a result expression of theswitchexpression.
It is a compile-time error if a switch expression has no result expressions.
A switch expression is a poly expression if it appears in an assignment context or an invocation context (§5.2, §5.3). Otherwise, it is a standalone expression.
Where a poly switch expression appears in a context of a particular kind with target type T, its result expressions similarly appear in a context of the same kind with target type T.
A poly switch expression is compatible with a target type T if each of its result expressions is compatible with T.
The type of a poly switch expression is the same as its target type.
The type of a standalone switch expression is determined as follows:
- If the result expressions all have the same type (which may be the null type (§4.1)), then that is the type of the
switchexpression. - Otherwise, if the type of each result expression is
booleanorBoolean, then an unboxing conversion (§5.1.8) is applied to each result expression of typeBoolean, and theswitchexpression has typeboolean. - Otherwise, if the type of each result expression is convertible to a numeric type (§5.1.8), then the type of the
switchexpression is the result of general numeric promotion (§5.6) applied to the result expressions. - Otherwise, boxing conversion (§5.1.7) is applied to each result expression that has a primitive type, after which the type of the
switchexpression is the result of applying capture conversion (§5.1.10) to the least upper bound (§4.10.4) of the types of the result expressions.
15.28.2. Run-Time Evaluation of switch Expressions
A switch expression is evaluated by first evaluating the selector expression. If evaluation of the selector expression completes abruptly, then evaluation of the entire switch expression completes abruptly for the same reason.
If evaluation of the selector expression completes normally then evaluation of the switch expression continues by determining if a switch label associated with the switch block applies to the value of the selector expression (§14.11.1.2). Then:
-
If the process of determining which switch label applies completes abruptly, then the entire
switchexpression completes abruptly for the same reason. -
If no switch label applies, then one of the following holds:
- If the value of the selector expression is
null, then aNullPointerExceptionis thrown and evaluation of theswitchexpression completes abruptly for that reason. - Otherwise, a
MatchExceptionis thrown and evaluation of theswitchexpression completes abruptly for that reason.
- If the value of the selector expression is
-
If a switch label applies, then one of the following holds:
- If it is the switch label for a switch rule expression, then the expression is evaluated. If the result of evaluation is a value, then the
switchexpression completes normally with the same value. - If it is the switch label for a switch rule block, then the block is executed. If this block completes normally, then the
switchexpression completes normally. - If it is the switch label for a switch rule
throwstatement, then thethrowstatement is executed. - Otherwise, all the statements in the switch block after the switch label that applies are executed in order. If these statements complete normally, then the
switchexpression completes normally.
- If it is the switch label for a switch rule expression, then the expression is evaluated. If the result of evaluation is a value, then the
If execution of any statement or expression in the switch block completes abruptly, it is handled as follows:
- If evaluation of an expression completes abruptly, then evaluation of the
switchexpression completes abruptly for the same reason. - If execution of a statement completes abruptly because of a
yieldwith value V, then evaluation of theswitchexpression completes normally and the value of theswitchexpression is V. - If execution of a statement completes abruptly for any reason other than a
yieldwith a value, then evaluation of theswitchexpression completes abruptly for the same reason.