CONVERSIONS AND PROMOTIONS
Unary Numeric Promotion
5.6.1
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
, or
char
, unary numeric
promotion promotes it to a value of type
int
by a widening conversion
( 5.1.2).
Otherwise, a unary numeric operand remains as is and is not converted.
Unary numeric promotion is performed on expressions in the following situations:
The dimension expression in array creations ( 15.9)
The index expression in array access expressions ( 15.12)
Operands of the unary operators plus
+
( 15.14.3) and minus
( 15.14.4)
The operand of the bitwise complement operator
~
( 15.14.5)
Each operand, separately, of the shift operators
>>
,
>>>
, 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
73
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved