|
1 | | -# java-pattern-matching |
| 1 | +# Pattern Matching in Java |
| 2 | + |
| 3 | +[](https://github.com/SvenWoltmann/java-pattern-matching/actions/workflows/build.yml) |
| 4 | +[](https://sonarcloud.io/dashboard?id=SvenWoltmann_java-pattern-matching) |
| 5 | +[](https://sonarcloud.io/dashboard?id=SvenWoltmann_java-pattern-matching) |
| 6 | +[](https://sonarcloud.io/dashboard?id=SvenWoltmann_java-pattern-matching) |
| 7 | + |
| 8 | +## Article |
| 9 | + |
| 10 | +The code refers to the following articles (not yet published): |
| 11 | +* English: [Pattern Matching in Java: instanceof, switch, and Record Patterns](https://www.happycoders.eu/java/pattern-matching/) |
| 12 | +* German: [Pattern Matching in Java: instanceof, switch und Record Patterns](https://www.happycoders.eu/de/java/pattern-matching/) |
| 13 | + |
| 14 | +## Contents |
| 15 | + |
| 16 | +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: |
| 17 | + |
| 18 | +``` |
| 19 | +java src/main/java/Intro.java |
| 20 | +``` |
| 21 | + |
| 22 | +`mvn compile` compiles all of them at once. |
| 23 | + |
| 24 | +The folders, in the order of the article: |
| 25 | + |
| 26 | +* `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()`) |
| 27 | +* `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. |
| 28 | +* `preview/` – primitive type patterns, a preview feature in Java 23 to 27: `java --enable-preview preview/PrimitiveTypePatterns.java` |
| 29 | + |
| 30 | +`SwitchWithoutCaseNull` ends with a `NullPointerException` on purpose: the article shows what a `switch` without `case null` does with `null`. |
| 31 | + |
| 32 | + |
| 33 | +<!-- happycoders-resources:start --> |
| 34 | +<!-- Generated from happycoders-website-astro/data/github-readme by scripts/content/sync-github-readmes.mjs. Edit there, not here. --> |
| 35 | + |
| 36 | +## <br>Additional Resources |
| 37 | + |
| 38 | +### <br>Java Versions PDF Cheat Sheet |
| 39 | + |
| 40 | +**Stay up-to-date** with the latest Java features with this **free** [PDF Cheat Sheet](https://www.happycoders.eu/java-versions/)! |
| 41 | + |
| 42 | +[<img src="/img/java-versions-cheat-sheet-mockup.png" alt="Java Versions PDF Cheat Sheet Mockup" width="468">](https://www.happycoders.eu/java-versions/) |
| 43 | + |
| 44 | +* Avoid lengthy research with this **concise overview of all Java versions from Java 10 to Java 27**. |
| 45 | +* **Discover the innovative features** of each new Java version, summarized on a single page. |
| 46 | +* **Impress your team** with your up-to-date knowledge of the latest Java version. |
| 47 | + |
| 48 | +👉 [Download the free Java Versions PDF](https://www.happycoders.eu/java-versions/)<br> |
| 49 | + |
| 50 | +_(Hier geht's zur deutschen Version → [Java-Versionen PDF](https://www.happycoders.eu/de/java-versionen/))_ |
| 51 | + |
| 52 | + |
| 53 | +### <br>The Big O Cheat Sheet |
| 54 | + |
| 55 | +With this **free** [1-page PDF cheat sheet](https://www.happycoders.eu/big-o-cheat-sheet/), you'll always have the **7 most important complexity classes** at a glance. |
| 56 | + |
| 57 | +[<img src="/img/big-o-cheat-sheet-mockup.png" alt="Big O PDF Cheat Sheet Mockup" width="304">](https://www.happycoders.eu/big-o-cheat-sheet/) |
| 58 | + |
| 59 | +* **Always choose the most efficient data structures** and thus increase the performance of your applications. |
| 60 | +* **Be prepared for technical interviews** and confidently present your algorithm knowledge. |
| 61 | +* **Become a sought-after problem solver** and be known for systematically tackling complex problems. |
| 62 | + |
| 63 | +👉 [Download the free Big O Cheat Sheet](https://www.happycoders.eu/big-o-cheat-sheet/)<br> |
| 64 | + |
| 65 | +_(Hier geht's zur deutschen Version → [O-Notation Cheat Sheet](https://www.happycoders.eu/de/o-notation-cheat-sheet/))_ |
| 66 | + |
| 67 | + |
| 68 | +### <br>HappyCoders Newsletter |
| 69 | +👉 Want to stay on top of modern Java? |
| 70 | +Sign up for the [HappyCoders newsletter](https://www.happycoders.eu/newsletter/) – Modern Java: new versions & features, performance, and JVM insights – once a month. |
| 71 | + |
| 72 | +_(Hier geht's zur deutschen Version → [HappyCoders-Newsletter deutsch](https://www.happycoders.eu/de/newsletter/))_ |
| 73 | + |
| 74 | + |
| 75 | +### <br>🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen |
| 76 | + |
| 77 | +Trage dich jetzt unverbindlich auf die [Warteliste](https://www.happycoders.eu/de/mastering-data-structures-warteliste/) von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot! |
| 78 | + |
| 79 | +[<img src="/img/mastering-data-structures-product-mockup.png" alt="Mastering Data Structures Mockup" width="640">](https://www.happycoders.eu/de/mastering-data-structures-warteliste/) |
| 80 | + |
| 81 | +👉 [Zur Warteliste](https://www.happycoders.eu/de/mastering-data-structures-warteliste/) |
| 82 | +<!-- happycoders-resources:end --> |
0 commit comments