Numeric contexts apply to the operands of an arithmetic operator.
Numeric contexts allow the use of:
- an identity conversion (§5.1.1)
- a widening primitive conversion (§5.1.2)
- a widening reference conversion (§5.1.5) 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)
- 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) and binary numeric promotion (§5.6.2).
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, orInteger, it is subjected to unboxing conversion (§5.1.8). The result is then promoted to a value of typeintby a widening primitive conversion (§5.1.2) or an identity conversion (§5.1.1). - Otherwise, if the operand is of compile-time type
Long,Float, orDouble, it is subjected to unboxing conversion (§5.1.8). - Otherwise, if the operand is of compile-time type
byte,short, orchar, it is promoted to a value of typeintby a widening primitive conversion (§5.1.2). - Otherwise, a unary numeric operand remains as is and is not converted.
After the conversion(s), if any, value set conversion (§5.1.13) 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)
-
The index expression in an array access expression (§15.10.3)
-
The operand of a unary plus operator
+(§15.15.3) -
The operand of a unary minus operator
-(§15.15.4) -
The operand of a bitwise complement operator
~(§15.15.5) -
Each operand, separately, of a shift operator
<<,>>, or>>>(§15.19).A
longshift distance (right operand) does not promote the value being shifted (left operand) tolong.
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:
-
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
-
Widening primitive conversion (§5.1.2) 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 todouble. - Otherwise, if either operand is of type
float, the other is converted tofloat. - Otherwise, if either operand is of type
long, the other is converted tolong. - Otherwise, both operands are converted to type
int.
- If either operand is of type
After the conversion(s), if any, value set conversion (§5.1.13) is then applied to each operand.
Binary numeric promotion is performed on the operands of certain operators:
- The multiplicative operators
*,/, and%(§15.17) - The addition and subtraction operators for numeric types
+and-(§15.18.2) - The numerical comparison operators
<,<=,>, and>=(§15.20.1) - The numerical equality operators
==and!=(§15.21.1) - The integer bitwise operators
&,^, and|(§15.22.1) - In certain cases, the conditional operator
? :(§15.25)
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.