Java Magazine, May/June 2019
ORACLE COM JAVAMAGAZINE MAY JUNE 2019 55 java 12 fall through and as a result can help reduce the scope for bugs Heres how youd refactor the previous code to make use of this new switch form switch event case PLAY System out println PLAY event case STOP System out println STOP event default System out println Unknown event This new switch form uses the lambda style syntax introduced in Java 8 consisting of the arrow between the label and the code that returns a value Note that these are not actual lambda expressions its just that the syntax is lambda like You can use single line expressions or curly braced blocks just as you can with the body of a lambda expression Heres an example that shows the syntax of mixing single line expressions and curly braced blocks switch event case PLAY System out println PLAY event counter case STOP System out println STOP event default System out println Unknown event Compound cases Next is dealing with multiple case labels Before Java 12 you could use only one label for each case For example in the following code despite the fact that the logic for STOP and PAUSE is the same youd need to handle two separate cases unless you use fall through switch event case PLAY
You must have JavaScript enabled to view digital editions.