Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Arithmetic Promotion

Fri Jan 30, 2009 1:46 pm

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.



Post a reply
  Related Posts  to : Arithmetic Promotion
 Web Promotion Mistakes     -  
 Arithmetic data comparison in C#     -  
 Arithmetic Operators in java     -  
 Results of Java expressions for arithmetic operations     -  
 Arithmetic data comparison and decompression java code     -  
 Program for "Selection of arithmetic operations" in Java     -  

Topic Tags

Java Basics