March 2019
Intermediate to advanced
242 pages
6h 21m
English
In a switch expression, when a switch branch isn't returning a value, it can execute a single statement, a block of statements, or even throw an exception. In the following example, the switch statement is not returning a value. It executes a single statement (prints out a value) for the case label, Sun; executes a block of code for the case labels, Mon and Tue; and throws an exception for its default case:
String day = // assign a value here switch(day) { case "Sun" -> System.out.println("OSS-Jav"); case "Mon", "Tue" -> { // some simple/complex code } default -> throw new RuntimeException("Running out of projects");}
Read now
Unlock full access