---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
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/se17/html/jls-14.html#jls-14.30
id: "14.30"
url: https://jvm.obfuscated.software/jls/se17/14.30.html
raw: https://jvm.obfuscated.software/jls/se17/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 local variables, 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 variable 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.

```
Pattern:
  TypePattern

TypePattern:
  LocalVariableDeclaration
```

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 type pattern declares one local variable, known as a pattern variable. The *Identifier* in the local variable declaration specifies the name of the 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* 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 is the reference type denoted by *LocalVariableType*.

The type of a type pattern is the type of its pattern variable.

An expression `e` is *compatible* with a pattern of type T if `e` is downcast compatible with T ([§5.5](5.5.md)).

Compatibility of an expression with a pattern is used by the `instanceof` pattern match operator ([§15.20.2](15.20.2.md)).

## 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)).

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

- A value `v` that is not the null reference *matches* a type pattern of type T if `v` can be cast to 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.

There is no rule to cover a value that *is* the null reference. This is because the solitary construct that performs pattern matching, the `instanceof` pattern match operator ([§15.20.2](15.20.2.md)), only does so when a value is *not* the null reference. It is possible that future versions of the Java programming language will allow pattern matching in other expressions and statements.
