Skip to content

About

Example code for the HappyCoders article “Pattern Matching in Java” – pattern matching for instanceof and switch, record patterns, exhaustiveness with sealed types, dominance, case null, and primitive type patterns (preview). Every listing is a runnable compact source file, plus the ones that don't compile.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

Pattern Matching in Java

Build Maintainability Rating Reliability Rating Security Rating

Article

The code refers to the following articles:

Contents

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), SwitchTypePatterns and SwitchCaseNull (Pattern Matching for switch), ExhaustiveSealed (a switch over a sealed interface without default), NestedRecordPattern and RecordPatternNull (record patterns), and Expressions (an expression tree of records with eval(), format() and simplify())
  • does-not-compile/ – the listings the article shows with a compiler error; the first line of each file names the error. Compile one with javac -d /tmp does-not-compile/ScopeWithOr.java to see it. The messages in the article come from javac 27; 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).


Additional Resources


Java Versions PDF Cheat Sheet

Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!

Java Versions PDF Cheat Sheet Mockup

  • 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)


The Big O Cheat Sheet

With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.

Big O PDF Cheat Sheet Mockup

  • 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)


HappyCoders Newsletter

👉 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!

Mastering Data Structures Mockup

👉 Zur Warteliste

About

Example code for the HappyCoders article “Pattern Matching in Java” – pattern matching for instanceof and switch, record patterns, exhaustiveness with sealed types, dominance, case null, and primitive type patterns (preview). Every listing is a runnable compact source file, plus the ones that don't compile.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages