Class PatternMatchingSwitchPreviewExamples

java.lang.Object
net.jrodolfo.java_evolution.java17.PatternMatchingSwitchPreviewExamples

public class PatternMatchingSwitchPreviewExamples extends Object
Demonstrates pattern matching for switch as a Java 17 preview feature.

Before pattern matching for switch, type-based branching often required a chain of if, instanceof, and casts. That scattered the dispatch logic across multiple statements.

Pattern matching for switch solves this by letting a switch branch on type patterns. It became final later, in Java 21. The executable method uses only type-pattern syntax that is faithful to both the Java 17 preview and the later feature; the README documents the historical guard syntax separately.

  • Constructor Details

    • PatternMatchingSwitchPreviewExamples

      public PatternMatchingSwitchPreviewExamples()
  • Method Details

    • describe

      public String describe(Object value)
      Uses type patterns inside a switch expression.
      Parameters:
      value - the value to classify
      Returns:
      a type-specific description