|
Besides assignment and methods call, the third context in which the type conversions take place is the numeric operations. In numeric operations, it is necessary to convert the operands to a common type so that the operations can be performed meaningfully. For example, following code performs an addition on two operands of different types byte and short:
byte b = 7;
short s = 9;
int summation = b + s; // addition on 2 different types
Though code looks straight forward, Java need to do few things in the background to get it working. The + operator is adding operands of different types and storing the result in yet another type. Behind the scene, Java converts the type of these operands into a common wider type so that the operation can be performed properly. These type conversions are known as numeric promotions as the values are promoted to a wider data type before the operations. While evaluating expression b + s in the above example, both byte and short values are first promoted to equivalent int values and then addition of two int values takes place.
|
|
Promotions do not change the magnitude of the value. They only change the type. For example, the byte value 8 when promoted to int is an int value 8. |
The Numeric promotions are applicable to the operands of all arithmetic operators. Hence, they are also known as the arithmetic promotions. There are two kinds of numeric promotions depending on whether the operator is unary or binary. The rules are different for unary and binary operators.
Numeric promotions with unary operators
Since the unary operators operate on a single value, the rules for promotions depend only on the type of this single value:
1. In case of unary operators +, - and ~ , if the operand is of type byte, short or char, it is promoted to int. If the operand is of any other type, it is not changed.
If you apply the unary + operator to a numeric value, the type of the resultant value in at least int. Following code will fail to compile as the type of +b is int.
byte b = 8;
byte c = +b; //Error! type of +b is int
The value +b should be stored in a int variable. The evaluation of expression +b will first promote the value b to int and then apply the operator + resulting in an int value.
2. In case of the unary operators ++ and --, no type conversion takes place.
If you apply the increment and decrement operators to a numeric value, the type of the resultant value remains unchanged. Following code increments a byte value.
byte b = 8;
byte c = b++; // Valid, type of b++ is byte
The value b++ can be store in a byte variable c without any problem as it is indeed a byte value.
Numeric promotions with binary operators
The numeric promotions are also applicable in binary operation. The binary operators operate on two values. Hence, the rules of type conversion depend on types of both values. Note that the numeric promotions are relevant to only non-boolean primitives. They are not applied to boolean operands. Java performs following steps in every binary operation involving the non-boolean operands.
1. If one of the operand is double, the other operand is promoted to double before the operation.
2. Else, if one of the operand is float, other is promoted to float before the operation
3. Else, if one of the operand is long, other is promoted to long before the operation.
4. Else, both operands are promoted to int before the operation.
Figure 10. 2 illustrates these steps pictorially.
![]() |
Figure 10.2 Steps for arithmetic promotion of primitives in binary operation
Whenever Java encounters any binary expression, it checks the type of operands and promotes their type if necessary. Figure 10.2 shows how Java determines the ultimate type of both the operands. Let us see how these steps are actually carried out with a code example. Following code has many binary expressions with operands of different data types:

|
|
The In the expression val1 * 2 – val3; first the multiplication val1 * 2 takes place. It is a multiplication between byte and int values. Therefore, the byte value is first promoted int value and then the multiplication takes place. The multiplication returns int value 16. Now next operator substraction has two operands, int 16 and val3. Since val3 is a float value, the int value 16 is promoted to float 16.0 and substraction 16.0 -8.0 takes place. The float value 8.0 is the result and it is assigned to float variable val3. that expects float
|
|||
|
|
In the expression val2 + val3, an addition between a int and float operand is taking place. Therefore int operand is promoted to float 8.0. Then the addition between two float values take place resulting in float value 16.0f. Now since it is to be assigned to a double variable, it is first converted into double. Therefore val4 will contain a double value 16.0.
|

Figure 10.3 illustrates the “behind the scene” steps performed by Java while
evaluating the expression val3 = val1 * 2 – val3; in the above code
example.
Figure 10.3 Step-wise arithmetic promotion of operands during the evaluation of expression
As you can see, lot is going on behind the scene. Before performing any binary operation, Java checks and if necessary promotes the type of the operands. If you remember the four rules illustrated in figure 10.2, it is easy to determine the value and type of any binary expression.
In last chapter we learned how the compound assignment operators (for example += or *=) perform implicit casting. This feature is particularly useful in case of operands that are promoted in the binary operation. For instance, a byte variable will be promoted to int in the expression a+1. If you want to store result back in the variable a, you need to cast it explicitly to byte as:
byte a = 8;
a = (byte) a + 3; // a + 3 is int due to promotion
However, if you use the += operator you need not do the casting as the operator += does it implicitly. Following code is valid
byte a=8;
a += 3; // valid due to implicit casting
The expression a+=3 is equivalent to a = (byte) a+3; therefore we can store result of addition in byte variable despite the promotion. All the compound operators perform implicit casting.