The code refers to the following articles:
- English: Pattern Matching in Java: instanceof, switch, and Record Patterns
- German: Pattern Matching in Java: instanceof, switch und Record Patterns
Every listing of the article is a compact source file of its own and prints the output shown there. The files need Java 25 or newer – compact source files and the IO class are final since Java 25 – even though the features they show are older. Run one directly from the repository root:
java src/main/java/Intro.java
mvn compile compiles all of them at once.
The folders, in the order of the article:
src/main/java/– the runnable listings, e.g.InstanceofPattern(Pattern Matching for instanceof),SwitchTypePatternsandSwitchCaseNull(Pattern Matching for switch),ExhaustiveSealed(a switch over a sealed interface withoutdefault),NestedRecordPatternandRecordPatternNull(record patterns), andExpressions(an expression tree of records witheval(),format()andsimplify())does-not-compile/– the listings the article shows with a compiler error; the first line of each file names the error. Compile one withjavac -d /tmp does-not-compile/ScopeWithOr.javato see it. The messages in the article come fromjavac27; up to Java 26, the "missing patterns" lines are not printed.preview/– primitive type patterns, a preview feature in Java 23 to 27:java --enable-preview preview/PrimitiveTypePatterns.java
Two files end with an exception on purpose, because the article shows it: InstanceofWrongCast with a ClassCastException (the test checks for Number, the cast assumes Integer), and SwitchWithoutCaseNull with a NullPointerException (a switch without case null receives null).
Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!
- Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
- Discover the innovative features of each new Java version, summarized on a single page.
- Impress your team with your up-to-date knowledge of the latest Java version.
👉 Download the free Java Versions PDF
(Hier geht's zur deutschen Version → Java-Versionen PDF)
With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.
- Always choose the most efficient data structures and thus increase the performance of your applications.
- Be prepared for technical interviews and confidently present your algorithm knowledge.
- Become a sought-after problem solver and be known for systematically tackling complex problems.
👉 Download the free Big O Cheat Sheet
(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)
👉 Want to stay on top of modern Java? Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.
(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)
🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen
Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!


