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. Alongshift distance (right operand) does not promote the value being shifted (left operand) tolong. - 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
switchexpression (§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:
-
If any expression is of a reference type, it is subjected to unboxing conversion (§5.1.8).
-
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 isdouble, and other expressions that are not of typedoubleundergo widening primitive conversion todouble. -
Otherwise, if any expression is of type
float, then the promoted type isfloat, and other expressions that are not of typefloatundergo widening primitive conversion tofloat. -
Otherwise, if any expression is of type
long, then the promoted type islong, and other expressions that are not of typelongundergo widening primitive conversion tolong. -
Otherwise, none of the expressions are of type
double,float, orlong. 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 typeintundergo widening primitive conversion toint.In a numeric choice context, the following rules apply:
- If any expression is of type
intand is not a constant expression (§15.29), then the promoted type isint, and other expressions that are not of typeintundergo widening primitive conversion toint. - Otherwise, if any expression is of type
short, and every other expression is either of typeshortor of typebyteor a constant expression of typeintwith a value that is representable in the typeshort, then the promoted type isshort, and thebyteexpressions undergo widening primitive conversion toshort, and theintexpressions undergo narrowing primitive conversion toshort. - Otherwise, if any expression is of type
byte, and every other expression is either of typebyteor a constant expression of typeintwith a value that is representable in the typebyte, then the promoted type isbyte, and theintexpressions undergo narrowing primitive conversion tobyte. - Otherwise, if any expression is of type
char, and every other expression is either of typecharor a constant expression of typeintwith a value that is representable in the typechar, then the promoted type ischar, and theintexpressions undergo narrowing primitive conversion tochar. - Otherwise, the promoted type is
int, and all the expressions that are not of typeintundergo widening primitive conversion toint.
- If any expression is of type
-
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.