Just add an L
to identify that one of the values is of primitive type long
:
System.out.println(365L * 24 * 3600 * 1024 * 1024 * 1024);
/* ^
* 'L' to indicate that one of the factors is long
*/
In Java, all math is done in the largest data type required to handle
all of the current values. So, if you have int * int, it will always
do the math as an integer, but int * long is done as a long.
In this case, the 1024*1024*1024*80 is done as an Int, which overflows
int.
The "L" of course forces one of the operands to be an Int-64 (long),
therefore all the math is done storing the values as a Long, thus no
overflow occurs.
Credit goes to Erich: https://stackoverflow.com/a/1494884/5645656
EDIT:
In many cases Java is based on C or C++ and these are based on
Assembly. An overflow/underflow is silent in C and C++ and almost
silent in assembly (unless you check special flags). This is likely
due to the fact that C and C++ didn't have exceptions when they were
first proposed. If you wanted to see overflows/underflows you just
used a larger type. e.g. long long int or long double ;) BTW assembly
has something similar to exceptions called traps or interrupts,
overflows/underflow doesn't cause a trap AFAIK.
Credit goes to Peter Lawrey: https://stackoverflow.com/a/15998029/5645656
TIP: Sometimes using google can answer your question before you ask it.