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

# 5.1.3. Narrowing Primitive Conversion

22 specific conversions on primitive types are called the *narrowing primitive conversions*:

- `short` to `byte` or `char`
- `char` to `byte` or `short`
- `int` to `byte`, `short`, or `char`
- `long` to `byte`, `short`, `char`, or `int`
- `float` to `byte`, `short`, `char`, `int`, or `long`
- `double` to `byte`, `short`, `char`, `int`, `long`, or `float`

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value, and may also lose precision and range.

A narrowing conversion of a signed integer to an integral type T simply discards all but the *n* lowest order bits, where *n* is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

A narrowing conversion of a `char` to an integral type T likewise simply discards all but the *n* lowest order bits, where *n* is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though chars represent 16-bit unsigned integer values.

A narrowing conversion of a floating-point number to an integral type T takes two steps:

1. In the first step, the floating-point number is converted either to a `long`, if T is `long`, or to an `int`, if T is `byte`, `short`, `char`, or `int`, as follows:

   - If the floating-point number is NaN ([§4.2.3](4.2.3.md)), the result of the first step of the conversion is an `int` or `long` `0`.

   - Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value `V` using the round toward zero rounding policy ([§4.2.4](4.2.4.md)). Then there are two cases:

     - (a) If T is `long`, and this integer value can be represented as a `long`, then the result of the first step is the `long` value `V`.
     - (b) Otherwise, if this integer value can be represented as an `int`, then the result of the first step is the `int` value `V`.

   - Otherwise, one of the following two cases must be true:

     - (a) The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type `int` or `long`.
     - (b) The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type `int` or `long`.

2. In the second step:

   - If T is `int` or `long`, the result of the conversion is the result of the first step.
   - If T is `byte`, `char`, or `short`, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.

A narrowing conversion from `double` to `float` occurs as determined by the rules of IEEE 754 for converting between binary floating-point formats, using the round to nearest rounding policy ([§15.4](15.4.md)). This conversion can lose precision, but also lose range, resulting in a `float` zero from a nonzero `double` and a `float` infinity from a finite `double`. A `double` NaN is converted to a `float` NaN and a `double` infinity is converted to the same-signed `float` infinity.

Despite the fact that overflow, underflow, or other loss of information may occur, a narrowing primitive conversion never results in a run-time exception ([§11.1.1](11.1.1.md)).

**Example 5.1.3-1. Narrowing Primitive Conversion**

```
class Test {
    public static void main(String[] args) {
        float fmin = Float.NEGATIVE_INFINITY;
        float fmax = Float.POSITIVE_INFINITY;
        System.out.println("long: " + (long)fmin +
                           ".." + (long)fmax);
        System.out.println("int: " + (int)fmin +
                           ".." + (int)fmax);
        System.out.println("short: " + (short)fmin +
                           ".." + (short)fmax);
        System.out.println("char: " + (int)(char)fmin +
                           ".." + (int)(char)fmax);
        System.out.println("byte: " + (byte)fmin +
                           ".." + (byte)fmax);
    }
}
```

This program produces the output:

```
long: -9223372036854775808..9223372036854775807
int: -2147483648..2147483647
short: 0..-1
char: 0..65535
byte: 0..-1
```

The results for `char`, `int`, and `long` are unsurprising, producing the minimum and maximum representable values of the type.

The results for `byte` and `short` lose information about the sign and magnitude of the numeric values and also lose precision. The results can be understood by examining the low order bits of the minimum and maximum `int`. The minimum `int` is, in hexadecimal, `0x80000000`, and the maximum int is `0x7fffffff`. This explains the `short` results, which are the low 16 bits of these values, namely, `0x0000` and `0xffff`; it explains the char results, which also are the low 16 bits of these values, namely, `'\u0000'` and `'\uffff'`; and it explains the byte results, which are the low 8 bits of these values, namely, `0x00` and `0xff`.

**Example 5.1.3-2. Narrowing Primitive Conversions that lose information**

```
class Test {
    public static void main(String[] args) {
        // A narrowing of int to short loses high bits:
        System.out.println("(short)0x12345678==0x" +
                           Integer.toHexString((short)0x12345678));
        // An int value too big for byte changes sign and magnitude:
        System.out.println("(byte)255==" + (byte)255);
        // A float value too big to fit gives largest int value:
        System.out.println("(int)1e20f==" + (int)1e20f);
        // A NaN converted to int yields zero:
        System.out.println("(int)NaN==" + (int)Float.NaN);
        // A double value too large for float yields infinity:
        System.out.println("(float)-1e100==" + (float)-1e100);
        // A double value too small for float underflows to zero:
        System.out.println("(float)1e-50==" + (float)1e-50);
    }
}
```

This program produces the output:

```
(short)0x12345678==0x5678
(byte)255==-1
(int)1e20f==2147483647
(int)NaN==0
(float)-1e100==-Infinity
(float)1e-50==0.0
```
