OpenJDK Terminology

Pattern Matching

July 03, 2026

Pattern matching is a set of language features that allow you to test the structure or type of a value and extract components from it in a single, concise expression. Java has added pattern matching incrementally since Java 14, progressively replacing verbose, error-prone type-checking idioms.

The simplest form is pattern matching for instanceof (finalised in Java 16): instead of writing if (obj instanceof String) { String s = (String) obj; }, you write if (obj instanceof String s). The cast and variable declaration are fused into the pattern.

The more powerful form is pattern matching in switch expressions and statements (finalised in Java 21). Combined with sealed classes, a switch can exhaustively match every subtype of a sealed hierarchy, and the compiler verifies at compile time that all cases are covered. This makes data-oriented programming in Java significantly more concise and safe.

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Comments (0)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

No comments yet. Be the first.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard