With switch expressions, Java 12 is enhancing one of its basic language constructs—switch—to improve the everyday coding experience of developers. The benefits of this are manifold. As compared to traditional switch constructs, switch expressions (JDK Enhancement Proposal (JEP) 325—http://openjdk.java.net/jeps/325) can return a value. The ability to define multiple constants with a switch branch, and improve code semantics, make it concise. By removing default fall through of control across switch branches, you are less likely to introduce a logical error in a switch expression.
In this chapter, you'll cover the following topics:
- Issues with existing switch statements
- The switch expression syntax
- Defining local variables in switch branches
- The extended break statement
- Comparing break with break <return value>
- Exhaustive cases...