What can a switch branch execute other than returning a value?

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");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.35.72