Class PatternMatchingSwitchExamples

java.lang.Object
net.jrodolfo.java_evolution.java21.PatternMatchingSwitchExamples

public class PatternMatchingSwitchExamples extends Object
Demonstrates pattern matching for switch, finalized in Java 21.

Before this feature, type-based dispatch usually meant an if chain with repeated instanceof checks and casts. Pattern matching for switch lets the switch expression branch directly on type patterns.

The feature is especially strong with sealed hierarchies because the compiler knows the permitted implementations and can help verify exhaustive handling.

  • Constructor Details

    • PatternMatchingSwitchExamples

      public PatternMatchingSwitchExamples()
  • Method Details

    • describe

      public String describe(PatternMatchingSwitchExamples.Command command)
      Handles a sealed command hierarchy with switch patterns.
      Parameters:
      command - the command to describe
      Returns:
      a command description