Change the type of maxSpace and usedSpace from int to long . If you cannot do this, simply change the code to something like
long freeSize = 1000L*1000*(Main.maxSPace - Main.usedSpace);
therefore, the result will be calculated as long , not int .
Now it computes like this:
Main.maxSPace-Main.usedSpace -> 19992 (Main.maxSPace-Main.usedSpace)*1000 -> 19992000 (Main.maxSPace-Main.usedSpace)*1000*1000L -> 19992000000
The problem is that we are working with integers, so the result should also be integer, but the maximum integer value is
2147483647 so 19992000000 is out of range
therefore Java will only take the last 32 bits of the result and change it to an integer
10010100111100111011011011000000000 -> 19992000000 10100111100111011011011000000000 -> -1482836480
Pshemo
source share