Pages

Java - Widening conversion (Automat type promotion) in Expressions

This post explains how automatic type conversion works in expressions.

Rule 1:

In all the below assignment expressions, Java automatically promotes(widens) byte, char, short data types to ‘int’ data type and after that source variable(right hand side variable) is cast to the data type of destination variable (left hand side variable). So, all these expressions combine both widening and narrowing conversion at the same time.

byte = (byte) char
byte = (byte) short
char = (char) byte
char = (char) short
short = (short) char

Rule 2:

In an arithmetic expression, if the data type of one of operands is any one of byte, char, short and the other operand is integer literal constant, or both the operands are of byte, char, short type, then each operand is automatically promoted to int data type before performing arithmetic operation on them. Below table summarizes this concept.

Operand 1
Operator
Operand 2
Result
byte, char, short
Arithmatic operator

byte, char, short
Each operand is promoted to int before the arithmetic operation
(+, -, *, /, %)
byte, char, short
Arithmatic operator
integer literal constant
Operand 1 is promoted to int before the arithmetic operation
(+, -, *, /, %)

Rule 3:

If one of the operand is a long, the other operand in the expression is promoted to long. If one of the operand is a float, the other operand in the expression is promoted to float. If one of the operand is a double, the other operand in the expression is promoted to double. Below table summarizes this concept.

Operand 1
Operator
Operand 2
Result
byte, char, short, int
Arithmatic operator
float literal constant
Operand 1 is promoted to float before the arithmetic operation
(+, -, *, /, %)
byte, char, short, int
Arithmatic operator
double literal constant
Operand 1 is promoted to double before the arithmetic operation
(+, -, *, /, %)
byte, char, short, int
Arithmatic operator
float
Operand 1 is promoted to float before the arithmetic operation
(+, -, *, /, %)
byte, char, short, int, float
Arithmatic operator
double
Operand 1 is promoted to double before the arithmetic operation
(+, -, *, /, %)

Example:-

Let us examine how the ‘result’ expression is evaluated in the below example.

class Test {
       public static void main(String arg[]) {
              byte byteVar = 100;
              char charVar = 'x';
              short shortVar = 120;
              int intVar = 110;
              float floatVar = 1.2F;
              double doubleVar = 1.3D;
              double result = (((charVar + byteVar + shortVar) * intVar) + floatVar) + doubleVar;
              System.out.println("Result : " + result);             
       }
}

Step 1:  Operands in the expression (charVar + byteVar) is automatically promoted int and addition is performed and the result of this expression is int.

Step 2:  shortVar is promoted to int and it is added to the result of Step-1 and the result of this expression is also an int

Step 3:  Result of Step-2 which is an int, is multiplied by intVar and the result of this expression is also an int

Step 4: Result of step-3 is promoted to float and it is added to floatVar and the result of this expression is a float

Step 5: Result of step-4 is promoted to double and it is added to doubleVar and result is a double

No comments :

Post a Comment