JVM Knowledge

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) 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, §8.3, §8.4.1, and §14.4 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 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. 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).

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

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) and expression evaluation (§15.1).

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