Class SwitchExpressionExamples
java.lang.Object
net.jrodolfo.java_evolution.java14.SwitchExpressionExamples
Demonstrates switch expressions, finalized in Java 14.
Before switch expressions, returning a value from switch usually
required mutable variables, repeated assignments, and careful
break statements.
Switch expressions solve this by allowing switch to produce a value
directly. They also support arrow labels, which avoid accidental
fall-through. Java 14 finalized the feature after previews in Java 12 and
Java 13.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
SwitchExpressionExamples
public SwitchExpressionExamples()
-
-
Method Details
-
season
Converts a month number into a season using a switch expression.- Parameters:
month- month number from 1 to 12- Returns:
- the season name, or
unknownfor invalid values
-
grade
Usesyieldwhen a switch branch needs a block with more than one statement.- Parameters:
score- numeric score- Returns:
- a grade label
-