CHAPTER 5: Conversions and Promotions Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

5.6 Numeric Promotions

5.6.1 Unary Numeric Promotion , 5.6.2 Binary Numeric Promotion

Numeric promotion is applied to the operands of an arithmetic operator. Numeric promotion contexts allow the use of an identity conversion (S5.1.1) or a widening primitive conversion (S5.1.2).

Numeric promotions are used to convert the operands of a numeric operator to a common type so that an operation can be performed. The two kinds of numeric promotion are unary numeric promotion (S5.6.1) and binary numeric promotion (S5.6.2). The analogous conversions in C are called "the usual unary conversions" and "the usual binary conversions."

Numeric promotion is not a general feature of Java, but rather a property of the specific definitions of the built-in operations.


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:

Unary numeric promotion is performed on expressions in the following situations:

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


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 of a numeric type, the following rules apply, in order, using widening conversion (S5.1.2) to convert operands as necessary:

Binary numeric promotion is performed on the operands of certain operators:

An example of binary numeric promotion appears above in S5.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);

	}

}

which 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.

Top© 1996 Sun Microsystems, Inc. All rights reserved.