Total members 11890 |It is currently Wed Apr 24, 2024 5:06 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Arithmetic Promotion
 Web Promotion Mistakes     -  
 Arithmetic Operators in java     -  
 Arithmetic data comparison in C#     -  
 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
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com