Java Reference ยท Object-Oriented Concepts
Pattern matching in Java
Pattern matching in Java: type patterns with instanceof, record deconstruction patterns, scope of pattern variables, null handling and Java version notes.
A pattern tests whether a value has a certain shape and, if it does, extracts parts of it into new variables in one step. Java's patterns replace the old "test with instanceof, then cast, then use" routine. Two kinds are in the standard language: type patterns (obj instanceof String s) and record patterns (obj instanceof Point(int x, int y)). Both also work as labels in switch.
Type patterns for instanceof were finalized in Java 16, and record patterns, along with pattern matching for switch, in Java 21.
if (obj instanceof String s && s.length() > 3) { System.out.println(s.toUpperCase()); // s is a String here, no cast }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Kinds of patterns
| Pattern | Example | Matches when |
|---|---|---|
| Type pattern | o instanceof String s |
o is non-null and a String; binds s |
| Record pattern | o instanceof Point(int x, int y) |
o is a Point; components match the sub-patterns |
| Nested record pattern | Line(Point(var a, var b), var to) |
each level matches |
var pattern |
Point(var x, var y) |
always matches the component; type is inferred |
Unnamed pattern _ |
Point(int x, _) |
ignores a component; standard since Java 22 |
Usage notes
nullnever matchesinstanceofpatterns. ABox(String s)pattern againstBox(null)also fails, sinceString sis a type test; but avar/total pattern would match a null component.- Scope of bindings: the variable is in scope only where the compiler can prove the match succeeded.
if (!(o instanceof String s)) return; s.length();works becausesis definitely assigned after theif. - Bindings are not final by default, but reassigning them is bad style. Do not reuse a binding name that is already a local variable.
- Generic types: you can only test a generic type if the cast is safe, e.g.
List<String>fromCollection<String>.o instanceof List<String>forObject ois rejected; useList<?>. - Always-true tests: since Java 21,
str instanceof String sis allowed even whenstris already aString; it simply matches any non-null value. - Record patterns call accessors, so a custom accessor that throws will propagate as
MatchExceptionin a switch. - Primitive patterns (
instanceof int ion anObjector boxed type) were still preview in Java 25; do not rely on them in production code. - Keep patterns simple: two nested levels read well; more usually signals missing helper methods.