Some operators apply unary numeric promotion to a single operand, which must produce a value of a numeric type:
byte
, short
, or char
, unary numeric promotion promotes it to a value of type int
by a widening conversion (§5.1.2).
Unary numeric promotion is performed on expressions in the following situations:
+
(§15.14.3) and minus -
(§15.14.4)
~
(§15.14.5)
>>
, >>>
, and <<
(§15.18), so that a long
shift distance (right operand) does not promote the value being shifted (left operand) to long
Here is a test program that includes examples of 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 test program produces the output:
a: -1,1 ~0xffffffff==0x0 0xffffffff<<4L==0xfffffff0