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. This project compiles on JDK 25, so the example uses final syntax while documenting the Java 17 preview origin.

  • 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