pattern-matching
9 tutorials tagged “pattern-matching”.
- Exhaustive Switch with Sealed TypesCover every subtype of a sealed interface without default, so the compiler flags each switch to update when a type is added. Includes an expression evaluator.
- Handling Null in SwitchWhy switch throws NullPointerException for null, how case null and case null, default fix it, and how null behaves in instanceof and record patterns.
- Pattern Matching for Switch in JavaSwitch on an object's type with case patterns instead of instanceof chains: type patterns, case order and dominance, exhaustiveness, and how it compiles.
- Pattern Variable Scope (Flow Scoping)Where can you use a pattern variable? Learn flow scoping: how the compiler decides, with if, else, &&, ||, the ternary operator, early returns and loops.
- Primitive Types in Patterns (Preview)A Java 25 preview: primitive type patterns in switch and instanceof, switching on long, double and boolean, and safe exactness checks for conversions.
- Record Patterns in JavaTake a record apart in one step with a record pattern: nested patterns, var, generics, record patterns in switch, exhaustiveness and the rules.
- Guards in Switch (When Clause)Add a condition to a case pattern with when: guarded patterns, their order, how they affect dominance and exhaustiveness, and the rules for guard expressions.
- Unnamed Variables and PatternsUse _ for variables and pattern parts you don't need: in loops, catch blocks, lambdas, try-with-resources, record patterns and multi-pattern cases.
- Java Instanceof and Pattern MatchingTest an object's type with instanceof, then let pattern matching declare the cast variable for you: binding variables, flow scoping and null.