Class SwitchExpressionExamples

java.lang.Object
net.jrodolfo.java_evolution.java14.SwitchExpressionExamples

public class SwitchExpressionExamples extends Object
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 Details

    • SwitchExpressionExamples

      public SwitchExpressionExamples()
  • Method Details

    • season

      public String season(int month)
      Converts a month number into a season using a switch expression.
      Parameters:
      month - month number from 1 to 12
      Returns:
      the season name, or unknown for invalid values
    • grade

      public String grade(int score)
      Uses yield when a switch branch needs a block with more than one statement.
      Parameters:
      score - numeric score
      Returns:
      a grade label