What is Arithmetic Promotion in Java? The arithmetic promotion in Java happens when you apply an arithmetic operation on two variables with different data-types. In this case the compiler will convert the data type of one operand in the binary arithmetic operation to the type of the other operand.
Here are some rules about arithmetic promotion in Java: - If one operand is of type "double", then the other operand is promoted to the type double.
- If Both operands are of type less than "int" , for example (byte,short,char...) .Both operands are promoted to type integer.
- If one of operands is type float and the other is not double .The other operand is converted to type float.
- If one of operands is type long and the other is not double or float .The other operand is converted to type long.
- If no operand of type (double,float ,long ) , then convert to type integer .
Note : All arithmetic promotion is done before calculations .
The result return type will be the common type created after the arithmetic promotion.
Note :Any arithmetic operations will be at least of type integer (int) .
For example :
java code
byte a=2;
byte b=4;
the result of a*b or a/b will be of type integer .
Last thing you must take in mind that you can't convert any type , there will be a situations you will need to use the cast operator <type> of java.