JVM Knowledge

Numeric contexts apply to the operands of arithmetic operators, array creation and access expressions, conditional expressions, and the result expressions of switch expressions.

An expression appears in a numeric arithmetic context if the expression is one of the following:

  • The operand of a unary plus operator +, unary minus operator -, or bitwise complement operator ~ (§15.15.3, §15.15.4, §15.15.5)
  • An operand of a multiplicative operator *, /, or % (§15.17)
  • An operand of an addition or subtraction operator for numeric types + or - (§15.18.2)
  • An operand of a shift operator <<, >>, or >>> (§15.19). Operands of these shift operators are treated separately rather than as a group. A long shift distance (right operand) does not promote the value being shifted (left operand) to long.
  • An operand of a numerical comparison operator <, <=, >, or >= (§15.20.1)
  • An operand of a numerical equality operator == or != (§15.21.1)
  • An operand of an integer bitwise operator &, ^, or | (§15.22.1)

An expression appears in a numeric array context if the expression is one of the following:

  • A dimension expression in an array creation expression (§15.10.1)
  • The index expression in an array access expression (§15.10.3)

An expression appears in a numeric choice context if the expression is one of the following:

  • The second or third operand of a numeric conditional expression (§15.25.2)
  • A result expression of a standalone switch expression (§15.28.1) where all the result expressions are convertible to a numeric type

Numeric promotion determines the promoted type of all the expressions in a numeric context. The promoted type is chosen such that each expression can be converted to the promoted type, and, in the case of an arithmetic operation, the operation is defined for values of the promoted type. The order of expressions in a numeric context is not significant for numeric promotion. The rules are as follows:

  1. If any expression is of a reference type, it is subjected to unboxing conversion (§5.1.8).

  2. Next, widening primitive conversion (§5.1.2) and narrowing primitive conversion (§5.1.3) are applied to some expressions, according to the following rules:

    • If any expression is of type double, then the promoted type is double, and other expressions that are not of type double undergo widening primitive conversion to double.

    • Otherwise, if any expression is of type float, then the promoted type is float, and other expressions that are not of type float undergo widening primitive conversion to float.

    • Otherwise, if any expression is of type long, then the promoted type is long, and other expressions that are not of type long undergo widening primitive conversion to long.

    • Otherwise, none of the expressions are of type double, float, or long. In this case, the kind of context determines how the promoted type is chosen.

      In a numeric arithmetic context or a numeric array context, the promoted type is int, and any expressions that are not of type int undergo widening primitive conversion to int.

      In a numeric choice context, the following rules apply:

      • If any expression is of type int and is not a constant expression (§15.29), then the promoted type is int, and other expressions that are not of type int undergo widening primitive conversion to int.
      • Otherwise, if any expression is of type short, and every other expression is either of type short or of type byte or a constant expression of type int with a value that is representable in the type short, then the promoted type is short, and the byte expressions undergo widening primitive conversion to short, and the int expressions undergo narrowing primitive conversion to short.
      • Otherwise, if any expression is of type byte, and every other expression is either of type byte or a constant expression of type int with a value that is representable in the type byte, then the promoted type is byte, and the int expressions undergo narrowing primitive conversion to byte.
      • Otherwise, if any expression is of type char, and every other expression is either of type char or a constant expression of type int with a value that is representable in the type char, then the promoted type is char, and the int expressions undergo narrowing primitive conversion to char.
      • Otherwise, the promoted type is int, and all the expressions that are not of type int undergo widening primitive conversion to int.

Unary numeric promotion consists of applying numeric promotion to a single expression that occurs in a numeric arithmetic context or a numeric array context.

Binary numeric promotion consists of applying numeric promotion to a pair of expressions that occur in a numeric arithmetic context.

General numeric promotion consists of applying numeric promotion to all the expressions that occur in a numeric choice context.

Example 5.6-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

Example 5.6-2. 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.