Skip to content
Tutorials Center
My learning

Topics

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.

Java
if (obj instanceof String s && s.length() > 3) {
    System.out.println(s.toUpperCase());   // s is a String here, no cast
}

Try it

Java Type patterns replace casts

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.

Java Record patterns deconstruct values

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

  • null never matches instanceof patterns. A Box(String s) pattern against Box(null) also fails, since String s is a type test; but a var/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 because s is definitely assigned after the if.
  • 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> from Collection<String>. o instanceof List<String> for Object o is rejected; use List<?>.
  • Always-true tests: since Java 21, str instanceof String s is allowed even when str is already a String; it simply matches any non-null value.
  • Record patterns call accessors, so a custom accessor that throws will propagate as MatchException in a switch.
  • Primitive patterns (instanceof int i on an Object or 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.