Java Pattern Puzzles
summary
Project Amber demos show concrete code (JShell plus OpenJDK JDK12/14/15/18/19/23) that adds pattern matching for instanceof, switch expressions, records, sealed classes and deconstruction; six puzzles expose surprises: JShell accepts reused variable name even though javac rejects it, guarded patterns use a contextual when token, record deconstructors forbid mixing a pattern variable with the deconstruction, sealed hierarchies let switches be exhaustive without default, and primitive promotion/boxing causes unexpected matches. Examples compile or fail exactly as shown; one JShell quirk behaves like a bug.
AI-written summary · about
chapters (20)
- 0:00Introduction
- 0:42Pattern matching fundamentals
- 2:30Switch expressions in Java
- 5:58Records and sealed classes
- 10:29Pattern matching for instanceof
- 16:30First puzzle: variable scoping
- 19:06Pattern matching for switch
- 24:17Guarded patterns explained
- 25:43Confusing guarded pattern
- 29:15Second puzzle: pattern dominance
- 32:29Record patterns and deconstruction
- 36:03Composing patterns
- 38:56Unnamed pattern variables
- 40:20Primitive types in patterns
- 42:05Third puzzle: autoboxing
- 45:23Switch versus instanceof
- 46:28Fourth puzzle: primitives
- 47:54Primitives with switch
- 48:59Fifth puzzle: completeness
- 49:21Summary and edge cases
