# Nice and Naughty Cases of Pattern Matching

DevFeed: [Nice and Naughty Cases of Pattern Matching](<https://devfeed.tech/articles/nice-and-naughty-cases-of-pattern-matching-23025.md>)

Original publisher: [Read original article](<https://www.javaadvent.com/2025/12/nice-and-naughty-cases-of-pattern-matching.html>)

Author: Cay Horstmann

Published: 2025-12-08T02:02:26Z

Content type: article

Language: en

Sources: [Java Advent Calendar](<https://devfeed.tech/sources/java-advent-calendar.md>)

Topics: [pattern matching](<https://devfeed.tech/topics/pattern-matching.md>), [exhaustive matching](<https://devfeed.tech/topics/exhaustive-matching.md>), [sealed interface](<https://devfeed.tech/topics/sealed-interface.md>)

Tags: [2025](<https://devfeed.tech/tags/2025.md>), [data](<https://devfeed.tech/tags/data.md>), [enum](<https://devfeed.tech/tags/enum.md>), [exhaustive](<https://devfeed.tech/tags/exhaustive.md>), [expression](<https://devfeed.tech/tags/expression.md>), [hierarchy](<https://devfeed.tech/tags/hierarchy.md>), [interface](<https://devfeed.tech/tags/interface.md>), [java](<https://devfeed.tech/tags/java.md>), [json](<https://devfeed.tech/tags/json.md>), [pattern-matching](<https://devfeed.tech/tags/pattern-matching.md>), [records](<https://devfeed.tech/tags/records.md>), [sealed](<https://devfeed.tech/tags/sealed.md>), [switch](<https://devfeed.tech/tags/switch.md>)

## AI overview

Java pattern matching is most useful when the data is modeled as a sealed hierarchy of interfaces and records; the excerpt says this style is worthwhile when it fits the problem domain. A JSON example represents leaves as records or finite enums and other types as sealed interfaces, then uses a switch expression with record and constant patterns. The switch is exhaustive, as switch expressions must cover every possible selector value. The excerpt introduces antipatterns and legacy switch corner cases but does not provide their details.

## Source excerpt

Since Java 14, the Java switch and instanceof statements have been enhanced, in multiple phases, to support pattern matching and a "data-oriented" programming style. In this article, I explore when this programming style is beneficial, and why. I look at the sweet spot of perfect pattern usage, absolute antipatterns where it should not be used, [...] The post Nice and Naughty Cases of Pattern Matching appeared first on JVM Advent.