---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 24 Edition"
java_version: 24
chapter: "Chapter 5. Conversions and Contexts"
section: "5.7"
heading: "5.7. Testing Contexts"
parent: "Chapter 5. Conversions and Contexts"
source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-5.html#jls-5.7
id: "5.7"
url: https://jvm.obfuscated.software/jls/se24/5.7.html
raw: https://jvm.obfuscated.software/jls/se24/5.7.md
---

# 5.7. Testing Contexts

*Testing contexts* allow the operand of a pattern match operator ([§15.20.2](15.20.2.md)), or the selector expression of a `switch` expression or statement that has at least one pattern `case` label associated with its switch block ([§14.11.1](14.11.1.md)) to be converted to a type as part of the process of pattern matching. As pattern matching is an inherently conditional process ([§14.30.2](14.30.2.md)), it is expected that a testing context will make use of conversions that may fail at run time.

Testing contexts use similar conversions for reference types as casting contexts except that they do not permit narrowing reference conversions that are unchecked ([§5.1.6.2](5.1.6.2.md)).

If the expression is of a primitive type, then a testing context allows the use of an identity conversion ([§5.1.1](5.1.1.md)).

If the expression is of a reference type, then a testing context allows the use of one of the following:

- an identity conversion ([§5.1.1](5.1.1.md))
- a widening reference conversion ([§5.1.5](5.1.5.md))
- a narrowing reference conversion that is checked ([§5.1.6.2](5.1.6.2.md))

If the expression has the null type, then the expression may be converted to any reference type.

If a testing context makes use of a narrowing reference conversion, then a run time check will be performed on the class of the expression's value, possibly causing a `ClassCastException`. Otherwise, no run time check is performed.
