Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
Arithmetic Promotion : This topic talk about Arithmetic promotion in Java.Suppose you have an arithmetic on two variables with different types.Here come what is called "Arithmetic promotion". In this case the compiler will convert the data type of one operand in the binary arithmetic operation to the type of the other.
Here is the rules of arithmetic promotion in Java language :
- One operand is of type "double" , then the other operand is promoted to the type double. - 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 :
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.
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )