While Java Development Kit (JDK) 11 won’t arrive for another month, work proceeds on its successor, JDK 12, with a switch expressions capability lined up for the release, expected in March 2019. Simplified coding is a goal of this capability.
Switch expressions, which will be in beta state in JDK 12, extends the switch
statement so it can be used as either a statement or an expression. This would enable both forms to use either “traditional” or “ simplified” scoping and control flow behavior. These changes would result in simplified “everyday” coding and prepare the way for use of pattern matching in switch
.
As Java builders move to support pattern matching, existing irregularities of the existing switch
statement become impediments. These include the default control flow behavior of switch blocks; default scoping of switch blocks, in which the block is treated as one single scope; and switch working only as a statement. The current design of Java’s switch
statement follows closely languages such as C++ and, by default, supports fallthrough semantics. This control flow has been useful for writing low-level code. But as switch is used in higher-level contexts, its error-prone nature begins to outweigh flexibility.
JDK 12 is now in development in the OpenJDK community. For now, the switch expressions proposal is the only technology formally listed as targetting JDK 12.