Files
javademos/patterns

patterns — Pattern matching from JDK 21 to 27

Companion code for the ankurm.com article Pattern Matching in Java: switch, Record Patterns and Primitive Patterns (JDK 21 to 27). The explanation lives in the article; this folder holds the runnable sources and the transcripts they produced.

JDK25=/path/to/jdk-25 JDK27=/path/to/jdk-27 [JDK21=/path/to/jdk-21] ./run.sh    # regenerates output/*.txt
  • src/ compiles and runs on 21, 25 and 27 (except Unnamed.java, which needs 22 or later).
  • broken/ fails to compile on purpose.
  • preview/ uses primitive patterns (JEP 532, preview in 27) and needs --enable-preview --release N.
  • sep/ is a sealed hierarchy compiled in two versions, to show the one runtime failure an exhaustive switch cannot prevent.

Tested on Temurin 25.0.4.1+1 and 27+35, and OpenJDK 21.0.10 for the baseline.