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

# 5.6. Numeric Contexts

*Numeric contexts* apply to the operands of an arithmetic operator.

Numeric contexts allow the use of:

- an identity conversion ([§5.1.1](5.1.1.md))
- a widening primitive conversion ([§5.1.2](5.1.2.md))
- a widening reference conversion ([§5.1.5](5.1.5.md)) followed by an unboxing conversion
- a widening reference conversion followed by an unboxing conversion, then followed by a widening primitive conversion
- an unboxing conversion ([§5.1.8](5.1.8.md))
- an unboxing conversion followed by a widening primitive conversion

A *numeric promotion* is a process by which, given an arithmetic operator and its argument expressions, the arguments are converted to an inferred target type T. T is chosen during promotion such that each argument expression can be converted to T and the arithmetic operation is defined for values of type T.

The two kinds of numeric promotion are unary numeric promotion ([§5.6.1](5.6.1.md)) and binary numeric promotion ([§5.6.2](5.6.2.md)).

## 5.6.1. Unary Numeric Promotion

Some operators apply *unary numeric promotion* to a single operand, which must produce a value of a numeric type:

- If the operand is of compile-time type `Byte`, `Short`, `Character`, or `Integer`, it is subjected to unboxing conversion ([§5.1.8](5.1.8.md)). The result is then promoted to a value of type `int` by a widening primitive conversion ([§5.1.2](5.1.2.md)) or an identity conversion ([§5.1.1](5.1.1.md)).
- Otherwise, if the operand is of compile-time type `Long`, `Float`, or `Double`, it is subjected to unboxing conversion ([§5.1.8](5.1.8.md)).
- Otherwise, if the operand is of compile-time type `byte`, `short`, or `char`, it is promoted to a value of type `int` by a widening primitive conversion ([§5.1.2](5.1.2.md)).
- Otherwise, a unary numeric operand remains as is and is not converted.

After the conversion(s), if any, value set conversion ([§5.1.13](5.1.13.md)) is then applied.

Unary numeric promotion is performed on expressions in the following situations:

- Each dimension expression in an array creation expression ([§15.10.1](15.10.1.md))

- The index expression in an array access expression ([§15.10.3](15.10.3.md))

- The operand of a unary plus operator `+` ([§15.15.3](15.15.3.md))

- The operand of a unary minus operator `-` ([§15.15.4](15.15.4.md))

- The operand of a bitwise complement operator `~` ([§15.15.5](15.15.5.md))

- Each operand, separately, of a shift operator `<<`, `>>`, or `>>>` ([§15.19](15.19.md)).

  A `long` shift distance (right operand) does not promote the value being shifted (left operand) to `long`.

**Example 5.6.1-1. Unary Numeric Promotion**

```
class Test {
    public static void main(String[] args) {
        byte b = 2;
        int a[] = new int[b];  // dimension expression promotion
        char c = '\u0001';
        a[c] = 1;              // index expression promotion
        a[0] = -c;             // unary - promotion
        System.out.println("a: " + a[0] + "," + a[1]);
        b = -1;
        int i = ~b;            // bitwise complement promotion
        System.out.println("~0x" + Integer.toHexString(b)
                           + "==0x" + Integer.toHexString(i));
        i = b << 4L;           // shift promotion (left operand)
        System.out.println("0x" + Integer.toHexString(b)
                           + "<<4L==0x" + Integer.toHexString(i));
    }
}
```

This program produces the output:

```
a: -1,1
~0xffffffff==0x0
0xffffffff<<4L==0xfffffff0
```

## 5.6.2. Binary Numeric Promotion

When an operator applies *binary numeric promotion* to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

1. If any operand is of a reference type, it is subjected to unboxing conversion ([§5.1.8](5.1.8.md)).

2. Widening primitive conversion ([§5.1.2](5.1.2.md)) is applied to convert either or both operands as specified by the following rules:

   - If either operand is of type `double`, the other is converted to `double`.
   - Otherwise, if either operand is of type `float`, the other is converted to `float`.
   - Otherwise, if either operand is of type `long`, the other is converted to `long`.
   - Otherwise, both operands are converted to type `int`.

After the conversion(s), if any, value set conversion ([§5.1.13](5.1.13.md)) is then applied to each operand.

Binary numeric promotion is performed on the operands of certain operators:

- The multiplicative operators `*`, `/`, and `%` ([§15.17](15.17.md))
- The addition and subtraction operators for numeric types `+` and `-` ([§15.18.2](15.18.2.md))
- The numerical comparison operators `<`, `<=`, `>`, and `>=` ([§15.20.1](15.20.1.md))
- The numerical equality operators `==` and `!=` ([§15.21.1](15.21.1.md))
- The integer bitwise operators `&`, `^`, and `|` ([§15.22.1](15.22.1.md))
- In certain cases, the conditional operator `? :` ([§15.25](15.25.md))

**Example 5.6.2-1. Binary Numeric Promotion**

```
class Test {
    public static void main(String[] args) {
        int i    = 0;
        float f  = 1.0f;
        double d = 2.0;
        // First int*float is promoted to float*float, then
        // float==double is promoted to double==double:
        if (i * f == d) System.out.println("oops");

        // A char&byte is promoted to int&int:
        byte b = 0x1f;
        char c = 'G';
        int control = c & b;
        System.out.println(Integer.toHexString(control));

        // Here int:float is promoted to float:float:
        f = (b==0) ? i : 4.0f;
        System.out.println(1.0/f);
    }
}
```

This program produces the output:

```
7
0.25
```

The example converts the ASCII character `G` to the ASCII control-G (BEL), by masking off all but the low 5 bits of the character. The `7` is the numeric value of this control character.
