Summary
Extend the switch statement so that it can be used as either a statement or an expression, and so that both forms can use either a traditional case :
label (with fall through) or a simplified case ->
label (no fall through).
Problem
Several irregularities of the switch statement have long been an irritation to users, and impediments to support for features like pattern matching. These irregularities include (i) the default control flow behavior (fall through) of switch blocks, (ii) the default scoping of switch blocks (the block is treated as one single scope), and (iii) the fact that switch works only as a statement, even though it is commonly more natural to express multi-way conditionals as expressions.
Solution
The JLS will be enhanced with support for:
switch
expressions- in
switch
expressions andswitch
statements, a choice between switch labeled rules (case ->
) and switch labeled statement groups (case :
). - in
switch
expressions andswitch
statements, the ability to have multiple constants in a case label. - in
switch
expressions, the introduction of a valuebreak
statement to yield a result from the switch expression.
These features, as a group, will constitute a preview feature (JEP 12) in Java SE 12.
Specification
Proposed changes to the JLS are attached to this CSR. The changes are also available at http://cr.openjdk.java.net/~gbierman/switch-expressions-2019-01.html. There are no changes to the JVMS.
- csr of
-
JDK-8206986 Compiler support for Switch Expressions (Preview)
- Resolved
- relates to
-
JDK-8192963 JEP 325: Switch Expressions (Preview)
- Closed
-
JDK-8207406 Compiler Tree API support for Switch Expressions (Preview)
- Closed
-
JDK-8223305 Compiler Support for Switch Expressions (Second Preview)
- Closed