Java int integer overflow – Programmer All
1 INT integer overflow 2 3 We calculate the number of microseconds in one day: 4 5 long microsPerDay = 24 * 60 * 60 * 1000 * 1000;// The correct result should be: 86400000000 6 System.out.println(microsPerDay);// In fact: 500654080 7 8 9 The problem is overflow during the calculation process. This calculation is completely executed in an int operation, and only after the operation is completed, the result is that it is too late: calculation has overflow. 10 Solution The first factor of the calculation expression is clearly the LONG type, so that all subsequent calculations in the expression can be done with the LONG operation, so that the result will not overflow: 11 12 long microsPerDay = 24L * 60 * 60 * 1000 * 1000;
Read more here: Source link