When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value of a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:
double, the other is converted to double.
float, the other is converted to float.
long, the other is converted to long.
int.
Binary numeric promotion is performed on the operands of certain operators:
*, / and % (§15.16)
+ and - (§15.17.2)
<, <=, >, and >= (§15.19.1)
== and != (§15.20.1)
&, ^, and | (§15.21.1)
? : (§15.24)
An example of binary numeric promotion appears above in §5.1. Here is another:
class Test {
public static void main(String[] args) {
int i = 0;
float f = 1.0f;
double d = 2.0;
// First i*f 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));
// A int:float promoted to float:float:
f = (b==0) ? f : 4.0f;
System.out.println(1.0/f);
}
}
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.
O suns! O grass of graves! O perpetual transfers and promotions!
Walt Whitman, Walt Whitman (1855)
in Leaves of Grass