Jul 3 2010

Numeric Promotions in Arithmetic Expressions

Java Programming Tips: Numeric Promotions in Arithmetic Expressions

Unary numeric promotion is applied to the single operand of unary arithmetic operators – and +. In other words, when a unary operator is applied to an operand of byte, short or char type, the operand is first promoted to a value of type int, with the evaluation resulting in an int value. If the conditions for implicit narrowing conversion are not fulfilled, assigning the int result to a variable of these types will require an explicit cast. This is demonstrated by the following example, where the byte operand b is promoted to an int in the expression (-b):

byte b = 3;           // int literal in range. Implicit narrowing.
b = (byte) -b;        // Explicit narrowing on assignment required.

Binary numeric promotion is applied to operands of binary arithmetic operators. Its application leads to automatic type promotion for the operands. The result is of the promoted type, which is always type int or wider.

1 Comments on this post

Trackbacks

  1. Java Programming Tips: Ternary Conditional Operator | Free The Internet wrote:

    [...] ternary conditional operator allows conditional expressions to be defined. The operator has the following [...]

    July 7th, 2010 at 6:38 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Spam Protection by WP-SpamFree