aspose file tools*
The moose likes Java in General and the fly likes data type of results of java arithmetic calculation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "data type of results of java arithmetic calculation" Watch "data type of results of java arithmetic calculation" New topic
Author

data type of results of java arithmetic calculation

Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 47
In java, I know the data type of the result of an arithmetic calculation depends on the data types of the numbers involved in the calculation. For example,

int + int = int

long/double=double

a. But I can't find any references which can give me all these rules. Could someone help me?

b. How to avoid over flow in arithmetic calculation? For example, the results of 2 long may not fit into a long anymore...

Thanks a lot.


Enjoy life!
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3454
    
  47

The JLS is the most authoritative source on the issue of type conversions in Java.

Java itself does not check integer arithmetic overflows, but I've found this interesting tool which does so.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: data type of results of java arithmetic calculation
 
Similar Threads
why ceil & floor accepts int
does == count as arithmetic calculation?
Tricky question.... plz help
Not Getting Compilation Error?
date type "Long" range too small?