Java Reference
In-Depth Information
• an unboxing conversion (§ 5.1.8 )
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 (§ 5.6.1 ) and binary numeric promotion (§ 5.6.2 ).
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
to unboxing conversion (§ 5.1.8 ) . The result is then promoted to a value of type int
by a widening primitive conversion (§ 5.1.2 ) or an identity conversion (§ 5.1.1 ) .
• Otherwise, if the operand is of compile-time type Long , Float , or Double , it is subjec-
ted to unboxing conversion (§ 5.1.8 ) .
• Otherwise, if the operand is of compile-time type byte , short , or char , it is promoted
to a value of type int by a widening primitive conversion (§ 5.1.2 ).
• Otherwise, a unary numeric operand remains as is and is not converted.
In any case, value set conversion (§ 5.1.13 ) is then applied.
Unary numeric promotion is performed on expressions in the following situations:
• Each dimension expression in an array creation expression (§ 15.10 )
• The index expression in an array access expression (§ 15.13 )
• The operand of a unary plus operator + 15.15.3 )
• The operand of a unary minus operator - 15.15.4 )
• The operand of a bitwise complement operator ~ 15.15.5 )
• Each operand, separately, of a shift operator >> , >>> , or << 15.19 ) .
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
Click here to view code image
class Test {
public static void main(String[] args) {
byte b = 2;
int a[] = new int[b]; // dimension expression promotion
Search WWH ::




Custom Search