Class SwitchExpressionPreviewExamples
java.lang.Object
net.jrodolfo.java_evolution.java12.SwitchExpressionPreviewExamples
Demonstrates switch expressions as a Java 12 preview feature.
Before switch expressions, returning a value from switch usually
required a mutable local variable and careful break statements.
Accidental fall-through was also a common source of bugs.
Java 12 previewed switch expressions to solve those problems. They let
switch produce a value directly and introduced arrow labels for
safer branch syntax. Switch expressions became final later, in Java 14. This
project compiles on JDK 25, so the example uses the final syntax while
documenting that Java 12 was the first preview release.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
SwitchExpressionPreviewExamples
public SwitchExpressionPreviewExamples()
-
-
Method Details
-
dayType
Uses a switch expression to return a value directly.- Parameters:
dayNumber- day number from 1 to 7- Returns:
- whether the day is a weekday or weekend
-