---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 24 Edition"
java_version: 24
chapter: "Chapter 14. Blocks, Statements, and Patterns"
section: "14.30"
heading: "14.30. Patterns"
parent: "Chapter 14. Blocks, Statements, and Patterns"
source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-14.html#jls-14.30
id: "14.30"
url: https://jvm.obfuscated.software/jls/se24/14.30.html
raw: https://jvm.obfuscated.software/jls/se24/14.30.md
---

# 14.30. Patterns

A *pattern* describes a test that can be performed on a value. Patterns appear as operands of statements and expressions, which provide the values to be tested. Patterns declare zero or more local variables, also known as pattern variables.

The process of testing a value against a pattern is known as *pattern matching*. If a value successfully matches a pattern, then the process of pattern matching initializes the pattern variables, if any, declared by the pattern.

Pattern variables are only in scope ([§6.3](6.3.md)) where pattern matching succeeds and thus the pattern variables will have been initialized. It is not possible to use a pattern variable that has not been initialized.

## 14.30.1. Kinds of Patterns

A *type pattern* is used to test whether a value is an instance of the type appearing in the pattern. A *record pattern* is used to test whether a value is an instance of a record class type and, if it is, to recursively perform pattern matching on the record component values.

```
Pattern:
  TypePattern
  RecordPattern

TypePattern:
  LocalVariableDeclaration

RecordPattern:
  ReferenceType ( [ComponentPatternList] )

ComponentPatternList:
  ComponentPattern {, ComponentPattern }

ComponentPattern:
  Pattern
  MatchAllPattern

MatchAllPattern:
  _
```

The following productions from [§4.3](4.3.md), [§8.3](8.3.md), [§8.4.1](8.4.1.md), and [§14.4](14.4.md) are shown here for convenience:

```
LocalVariableDeclaration:
  {VariableModifier} LocalVariableType VariableDeclaratorList

VariableModifier:
  Annotation
  final

LocalVariableType:
  UnannType
  var

VariableDeclaratorList:
  VariableDeclarator {, VariableDeclarator}

VariableDeclarator:
  VariableDeclaratorId [= VariableInitializer]

VariableDeclaratorId:
  Identifier [Dims]
  _

Dims:
  {Annotation} [ ] {{Annotation} [ ]}
```

See [§8.3](8.3.md) for *UnannType*.

A pattern is *nested* in a record pattern if (1) it appears directly in the component pattern list of the record pattern, or (2) it is nested in a record pattern that appears directly in the component pattern list of the record pattern. A pattern is *top level* if it is not nested in a record pattern.

A type pattern declares one local variable, known as a pattern variable. If the declaration includes an identifier then this specifies the name of the pattern variable, otherwise the pattern variable is called an *unnamed pattern variable*.

The rules for a local variable declared in a type pattern are specified in [§14.4](14.4.md). In addition, all of the following must be true, or a compile-time error occurs:

- The *LocalVariableType* in a top level type pattern denotes a reference type (and furthermore is not `var`).
- The *VariableDeclaratorList* consists of a single *VariableDeclarator*.
- The *VariableDeclarator* has no initializer.
- The *VariableDeclaratorId* has no bracket pairs.

The type of a pattern variable declared in a top level type pattern is the reference type denoted by *LocalVariableType*.

The type of a pattern variable declared in a nested type pattern is determined as follows:

- If the *LocalVariableType* is *UnannType* then the type of the pattern variable is denoted by *UnannType*.

- If the *LocalVariableType* is `var` then the type pattern must appear directly in the component pattern list of a record pattern, or a compile-time error occurs.

  Let R be the type of the record pattern, and let T be the type of the corresponding component field in R ([§8.10.3](8.10.3.md)). The type of the pattern variable is the upward projection of T with respect to all synthetic type variables mentioned by T.

  Consider the following declaration of a record class:

  ```
  record R<T>(ArrayList<T> a){}
  ```

  Given the record pattern `R`<`String`>`(var b)`, the type of the pattern variable `b` is `ArrayList`<`String`>.

A type pattern is said to be *null matching* if it is appears directly in the component pattern list of a record pattern with type R, where the corresponding record component of R has type U, and the type pattern is unconditional for the type U ([§14.30.3](14.30.3.md)).

Note that this compile-time property of type patterns is used in the run-time process of pattern matching ([§14.30.2](14.30.2.md)), so it is associated with the type pattern for use at run time.

A record pattern consists of a *ReferenceType* and a component pattern list containing component patterns, if any. If *ReferenceType* is not a record class type ([§8.10](8.10.md)) then a compile-time error occurs.

If the *ReferenceType* is a raw type, then the type of the record pattern is inferred, as described in [§18.5.5](18.5.5.md). It is a compile-time error if no type can be inferred for the record pattern.

If the *ReferenceType* (or any part of it) is annotated then a compile-time error occurs.

Future versions of the Java Programming Language may lift this restriction on annotations.

Otherwise, the type of the record pattern is *ReferenceType*.

The length of the record pattern's component pattern list must be the same as the length of the record component list in the declaration of the record class named by *ReferenceType* otherwise a compile-time error occurs.

A record pattern does not directly declare any pattern variables itself, but may contain declarations of pattern variables in the component pattern list.

It is a compile-time error if a record pattern contains more than one declaration of a pattern variable with the same name.

The *match-all pattern* is a special pattern that declares no pattern variables and can only appear directly in the component pattern list of a record pattern `r`.

Let R be the type of the record pattern `r`, and let T be the type of the corresponding component field in R ([§8.10.3](8.10.3.md)). The type of the match-all pattern is the upward projection of T with respect to all synthetic type variables mentioned by T.

It can be seen that a match-all pattern is equivalent to a nested type pattern that declares an *unnamed* pattern variable and whose *LocalVariableType* is `var`.

## 14.30.2. Pattern Matching

*Pattern matching* is the process of testing a value against a pattern at run time. Pattern matching is distinct from statement execution ([§14.1](14.1.md)) and expression evaluation ([§15.1](15.1.md)). If a value successfully matches a pattern, then the process of pattern matching will initialize all the pattern variables declared by the pattern, if any.

The process of pattern matching may involve expression evaluation or statement execution. Accordingly, pattern matching is said to *complete abruptly* if evaluation of an expression or execution of a statement completes abruptly. An abrupt completion always has an associated reason, which is always a `throw` with a given value. Pattern matching is said to *complete normally* if it does not complete abruptly.

The rules for determining whether a value matches a pattern, and for initializing pattern variables, are as follows:

- The null reference *matches* a type pattern if the type pattern is null-matching ([§14.30.1](14.30.1.md)); and *does not match* otherwise.

  If the null reference matches, then the pattern variable declared by the type pattern is initialized to the null reference.

  If the null reference does not match, then the pattern variable declared by the type pattern is not initialized.

- A value `v` that is not the null reference *matches* a type pattern of type T if `v` can be converted by testing conversion ([§5.7](5.7.md)) to the target type T without raising a `ClassCastException`; and *does not match* otherwise.

  If `v` matches, then the pattern variable declared by the type pattern is initialized to `v`.

  If `v` does not match, then the pattern variable declared by the type pattern is not initialized.

- The null reference *does not match* a record pattern.

  In this case, any pattern variables appearing in declarations contained in the record pattern are not initialized.

- A value `v` that is not the null reference *matches* a record pattern with type R and component pattern list `L` if (i) `v` can be converted by testing conversion ([§5.7](5.7.md)) to the target type R without raising a `ClassCastException`; and (ii) each record component of `v` matches the corresponding component pattern in `L`; and *does not match* otherwise.

  Each record component of `v` is determined by invoking the accessor method of `v` corresponding to that component. If execution of the invocation of the accessor method completes abruptly for reason S, then pattern matching completes abruptly by throwing a `MatchException` with cause S.

  A pattern variable declared by a pattern appearing in the component pattern list of a record pattern is initialized only if *all* the patterns in the list match.

- Every value *matches* a match-all pattern.

## 14.30.3. Properties of Patterns

A pattern `p` is said to be *applicable* at a type T if one of the following rules apply:

- A type pattern that declares a pattern variable of a reference type U is applicable at a reference type T if there is a testing conversion ([§5.7](5.7.md)) from type T to type U.
- A type pattern that declares a pattern variable of a primitive type P is applicable at the type P.
- A record pattern with type R and pattern list `L` is applicable at type T if (i) there is a testing conversion ([§5.7](5.7.md)) from type T to type R, and (ii) for every component pattern `p` appearing in `L`, if any, `p` is applicable at the type of the corresponding component field in R.
- A match-all pattern is applicable at every type T.

A pattern `p` is said to be *unconditional* for a type T if it can be determined at compile time that every value of type T will match `p`, and so the run time, testing aspect of pattern matching could be elided. It is defined as follows:

- A type pattern that declares a pattern variable of a reference type S is unconditional for a reference type T if the erasure of T is a subtype of the erasure of S.
- A type pattern that declares a pattern variable of a primitive type P is unconditional for the type P.
- A match-all pattern is unconditional for every type T.

Note that no record pattern is unconditional because the null reference does not match any record pattern.

A pattern `p` is said to *dominate* another pattern `q` if every value that matches `q` also matches `p`, and is defined as follows:

- A pattern `p` dominates a type pattern that declares a pattern variable of type T if `p` is unconditional for T.
- A pattern `p` dominates a record pattern with type R if `p` is unconditional for R.
- A record pattern with type R and component pattern list `L` dominates another record pattern with type S and component pattern list `M` if (i) R and S name the same record class, and (ii) every component pattern, if any, in `L` dominates the corresponding component pattern in `M`.
- A pattern `p` dominates a match-all pattern with type T if `p` is unconditional for T.
