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

# 5.1.6. Narrowing Reference Conversion

A *narrowing reference conversion* treats expressions of a reference type S as expressions of a different reference type T, where S is not a subtype of T. The supported pairs of types are defined in [§5.1.6.1](5.1.6.1.md). Unlike widening reference conversion, the types need not be directly related. However, there are restrictions that prohibit conversion between certain pairs of types when it can be statically proven that no value can be of both types.

A narrowing reference conversion may require a test at run time to validate that a value of type S is a legitimate value of type T. However, due to the lack of parameterized type information at run time, some conversions cannot be fully validated by a run time test; they are flagged at compile time ([§5.1.6.2](5.1.6.2.md)). For conversions that can be fully validated by a run time test, and for certain conversions that involve parameterized type information but can still be partially validated at run time, a `ClassCastException` is thrown if the test fails ([§5.1.6.3](5.1.6.3.md)).

## 5.1.6.1. Allowed Narrowing Reference Conversion

A narrowing reference conversion exists from reference type S to reference type T if all of the following are true:

- S is not a subtype of T ([§4.10](4.10.md))

- If there exists a parameterized type X that is a supertype of T, and a parameterized type Y that is a supertype of S, such that the erasures of X and Y are the same, then X and Y are not provably distinct ([§4.5](4.5.md)).

  Using types from the `java.util` package as an example, no narrowing reference conversion exists from `ArrayList<String>` to `ArrayList<Object>`, or vice versa, because the type arguments `String` and `Object` are provably distinct. For the same reason, no narrowing reference conversion exists from `ArrayList<String>` to `List<Object>`, or vice versa. The rejection of provably distinct types is a simple static gate to prevent "stupid" narrowing reference conversions.

- One of the following cases applies:

  1. S is a class or interface type, and T is a class or interface type, and S names a class or interface that is not disjoint from the class or interface named by T. ("disjoint" is defined below.)
  2. S is the class type `Object` or the interface type `java.io.Serializable` or `Cloneable` (the only interfaces implemented by arrays ([§10.8](10.8.md))), and T is an array type.
  3. S is an array type SC`[]`, that is, an array of components of type SC; T is an array type TC`[]`, that is, an array of components of type TC; and a narrowing reference conversion exists from SC to TC.
  4. S is a type variable, and a narrowing reference conversion exists from the upper bound of S to T.
  5. T is a type variable, and either a widening reference conversion or a narrowing reference conversion exists from S to the upper bound of T.
  6. S is an intersection type S_1 `&` ... `&` S_n, and for all *i* (1 ≤ *i* ≤ *n*), either a widening reference conversion or a narrowing reference conversion exists from S_i to T.
  7. T is an intersection type T_1 `&` ... `&` T_n, and for all *i* (1 ≤ *i* ≤ *n*), either a widening reference conversion or a narrowing reference conversion exists from S to T_i.

A class or interface is *disjoint* from another class or interface if it can be determined statically that they have no instances in common (other than the `null` value). The rules for disjointess are as follows:

- A class named C is disjoint from an interface named I if (i) it is not the case that C `<:` I, and (ii) one of the following cases applies:

  - C is `final`.
  - C is `sealed`, and all of the permitted direct subclasses of C are disjoint from I.
  - C is freely extensible ([§8.1.1.2](8.1.1.2.md)), and I is `sealed`, and C is disjoint from all of the permitted direct subclasses and subinterfaces of I.

- An interface named I is disjoint from a class named C if C is disjoint from I.

- A class named C is disjoint from another class named D if (i) it is not the case that C `<:` D, and (ii) it is not the case that D `<:` C.

- An interface named I is disjoint from another interface named J if (i) it is not that case that I `<:` J, and (ii) it is not the case that J `<:` I, and (iii) one of the following cases applies:

  - I is `sealed`, and all of the permitted direct subclasses and subinterfaces of I are disjoint from J.
  - J is `sealed`, and I is disjoint from all the permitted direct subclasses and subinterfaces of J.

Whether a class is `final` has the most bearing on whether the class is disjoint from interfaces. Consider the following declarations:

```
interface I   {}
final class C {}
```

As class C is `final` and does not implement I, there can be no instances of C that are also an instance of I, so C and I are disjoint. Therefore, there is no narrowing reference conversion from C to I.

In contrast, consider the following declarations:

```
interface J {}
class D     {}
```

Even though class D does not implement J, it is still possible for an instance of D to be an instance of J, for example, if the following declaration occurs:

```
class E extends D implements J {}
```

For this reason, D is not disjoint from J, and there is a narrowing reference conversion from D to J.

The final clause above implies that two freely extensible interfaces ([§9.1.1.4](9.1.1.4.md)) are not disjoint.

## 5.1.6.2. Checked and Unchecked Narrowing Reference Conversions

A narrowing reference conversion is either *checked* or *unchecked*. These terms refer to the ability of the Java Virtual Machine to validate, or not, the type correctness of the conversion.

If a narrowing reference conversion is unchecked, then the Java Virtual Machine will not be able to fully validate its type correctness, possibly leading to heap pollution ([§4.12.2](4.12.2.md)). To flag this to the programmer, an unchecked narrowing reference conversion causes a compile-time *unchecked warning*, unless suppressed by `@SuppressWarnings` ([§9.6.4.5](9.6.4.5.md)). Conversely, if a narrowing reference conversion is not unchecked, then it is checked; the Java Virtual Machine will be able to fully validate its type correctness, so no warning is given at compile time.

The unchecked narrowing reference conversions are as follows:

- A narrowing reference conversion from a type S to a parameterized class or interface type T is unchecked, unless at least one of the following is true:

  - All of the type arguments of T are unbounded wildcards.
  - T `<:` S, and S has no subtype X other than T where the type arguments of X are not contained in the type arguments of T.

- A narrowing reference conversion from a type S to a type variable T is unchecked.

- A narrowing reference conversion from a type S to an intersection type T_1 `&` ... `&` T_n is unchecked if there exists a T_i (1 ≤ *i* ≤ *n*) such that S is not a subtype of T_i and a narrowing reference conversion from S to T_i is unchecked.

## 5.1.6.3. Narrowing Reference Conversions at Run Time

All *checked* narrowing reference conversions require a validity check at run time. Primarily, these conversions are to class and interface types that are not parameterized.

Some *unchecked* narrowing reference conversions require a validity check at run time. This depends on whether the unchecked narrowing reference conversion is *completely unchecked* or *partially unchecked*. A partially unchecked narrowing reference conversion requires a validity check at run time, while a completely unchecked narrowing reference conversion does not.

These terms refer to the compatibility of the types involved in the conversion *when viewed as raw types*. If the conversion is conceptually an "upcast", then the conversion is *completely unchecked*; no run time test is needed because the conversion is legal in the non-generic type system of the Java Virtual Machine. Conversely, if the conversion is conceptually a "downcast", then the conversion is *partially unchecked*; even in the non-generic type system of the Java Virtual Machine, a run time check is needed to test the compatibility of the (raw) types involved in the conversion.

Using types from the `java.util` package as an example, a conversion from `ArrayList<String>` to `Collection<T>` is completely unchecked, because the (raw) type `ArrayList` is a subtype of the (raw) type `Collection` in the Java Virtual Machine. Conversely, a conversion from `Collection<T>` to `ArrayList<String>` is partially unchecked, because the (raw) type `Collection` is not a subtype of the (raw) type `ArrayList` in the Java Virtual Machine.

The categorization of an unchecked narrowing reference conversion is as follows:

- An unchecked narrowing reference conversion from S to a non-intersection type T is completely unchecked if |S| `<:` |T|.

  Otherwise, it is partially unchecked.

- An unchecked narrowing reference conversion from S to an intersection type T_1 `&` ... `&` T_n is completely unchecked if, for all *i* (1 ≤ *i* ≤ *n*), either S `<:` T_i or a narrowing reference conversion from S to T_i is completely unchecked.

  Otherwise, it is partially unchecked.

The run time validity check for a checked or partially unchecked narrowing reference conversion is as follows:

- If the value at run time is `null`, then the conversion is allowed.

- Otherwise, let R be the class of the object referred to by the value, and let T be the erasure ([§4.6](4.6.md)) of the type being converted to. Then:

  - If R is an ordinary class (not an array class):

    - If T is a class type, then R must be either the same class as T ([§4.3.4](4.3.4.md)) or a subclass of T, or a `ClassCastException` is thrown.
    - If T is an interface type, then R must implement interface T ([§8.1.5](8.1.5.md)), or a `ClassCastException` is thrown.
    - If T is an array type, then a `ClassCastException` is thrown.

  - If R is an interface:

    Note that R cannot be an interface when these rules are first applied for any given conversion, but R may be an interface if the rules are applied recursively because the run-time reference value may refer to an array whose element type is an interface type.

    - If T is a class type, then T must be `Object` ([§4.3.2](4.3.2.md)), or a `ClassCastException` is thrown.
    - If T is an interface type, then R must be either the same interface as T or a subinterface of T, or a `ClassCastException` is thrown.
    - If T is an array type, then a `ClassCastException` is thrown.

  - If R is a class representing an array type RC`[]`, that is, an array of components of type RC:

    - If T is a class type, then T must be `Object` ([§4.3.2](4.3.2.md)), or a `ClassCastException` is thrown.
    - If T is an interface type, then T must be the type `java.io.Serializable` or `Cloneable` (the only interfaces implemented by arrays), or a `ClassCastException` is thrown.
    - If T is an array type TC`[]`, that is, an array of components of type TC, then a `ClassCastException` is thrown unless either TC and RC are the same primitive type, or TC and RC are reference types and are allowed by a recursive application of these run-time rules.

If the conversion is to an intersection type T_1 `&` ... `&` T_n, then for all *i* (1 ≤ *i* ≤ *n*), any run-time check required for a conversion from S to T_i is also required for the conversion to the intersection type.
