Strange Java Math Result

Really a simple question, but maybe someone can explain. I have 2 lines of code:

long millisPerYear = 365*24*60*60*1000; System.out.println("millis per year = " + millisPerYear); 

I expect an exit of 31,536,000,000, but I get 1,471,228,928.

If I remove 1000 from the formula, the answer is correct, but 1000 pushes it to the edge.

The format of the variables is Long , so the size should be 2 64 large enough. I am at a standstill why the values ​​are not stored exactly.

+7
java math
source share
1 answer

You do the calculations on the right side using only int s. Replace 365 with 365L to do the calculation in long .

+16
source share

All Articles