---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 15. Expressions"
section: "15.16"
heading: "15.16. Cast Expressions"
parent: "Chapter 15. Expressions"
source: https://docs.oracle.com/javase/specs/jls/se27/html/jls-15.html#jls-15.16
id: "15.16"
url: https://jvm.obfuscated.software/jls/se27/15.16.html
raw: https://jvm.obfuscated.software/jls/se27/15.16.md
---

# 15.16. Cast Expressions

A cast expression converts, at run time, a value of one numeric type to a similar value of another numeric type; or confirms, at compile time, that the type of an expression is `boolean`; or checks, at run time, that a reference value refers to an object either whose class is compatible with a specified reference type or list of reference types, or which embodies a value of a primitive type.

```
CastExpression:
  ( PrimitiveType ) UnaryExpression
  ( ReferenceType {AdditionalBound} ) UnaryExpressionNotPlusMinus
  ( ReferenceType {AdditionalBound} ) LambdaExpression
```

The following production from [§4.4](4.4.md) is shown here for convenience:

```
AdditionalBound:
  & InterfaceType
```

The parentheses and the type or list of types they contain are sometimes called the *cast operator*.

If the cast operator contains a list of types, that is, a *ReferenceType* followed by one or more *AdditionalBound* terms, then all of the following must be true, or a compile-time error occurs:

- *ReferenceType* must denote a class or interface type.
- The erasures ([§4.6](4.6.md)) of all the listed types must be pairwise different.
- No two listed types may be subtypes of different parameterizations of the same generic interface.

The target type for the casting context ([§5.5](5.5.md)) introduced by the cast expression is either the *PrimitiveType* or the *ReferenceType* (if not followed by *AdditionalBound* terms) appearing in the cast operator, or the intersection type denoted by the *ReferenceType* and *AdditionalBound* terms appearing in the cast operator.

The type of a cast expression is the result of applying capture conversion ([§5.1.10](5.1.10.md)) to this target type.

Casts can be used to explicitly "tag" a lambda expression or a method reference expression with a particular target type. To provide an appropriate degree of flexibility, the target type may be a list of types denoting an intersection type, provided the intersection induces a functional interface ([§9.8](9.8.md)).

The result of a cast expression is not a variable, but a value, even if the result of evaluating the operand expression is a variable.

If the compile-time type of the operand cannot be converted by casting conversion ([§5.5](5.5.md)) to the target type specified by the cast operator, then a compile-time error occurs.

Otherwise, at run time, the operand value is converted (if necessary) by casting conversion to the target type specified by the cast operator.

A `ClassCastException` is thrown if a cast is found at run time to be impermissible.

Some casts result in an error at compile time. Some casts can be proven, at compile time, always to be correct at run time. For example, it is always correct to convert a value of a class type to the type of its superclass; such a cast should require no special action at run time. Finally, some casts cannot be proven to be either always correct or always incorrect at compile time. Such casts require a test at run time. See [§5.5](5.5.md) for details.
