Java Reference
In-Depth Information
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
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
, or
Integer
, it is subjected
• Otherwise, if the operand is of compile-time type
Long
,
Float
, or
Double
, it is subjec-
• Otherwise, if the operand is of compile-time type
byte
,
short
, or
char
, it is promoted
• Otherwise, a unary numeric operand remains as is and is not converted.
Unary numeric promotion is performed on expressions in the following situations:
• The index expression in an array access expression (§
15.13
)
A
long
shift distance (right operand) does not promote the value being shifted (left
operand) to
long
.
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