How does Java handle integer underflows and overflows and how would you check for it?
问题 How does Java handle integer underflows and overflows? Leading on from that, how would you check/test that this is occurring? 回答1: If it overflows, it goes back to the minimum value and continues from there. If it underflows, it goes back to the maximum value and continues from there. You can check that beforehand as follows: public static boolean willAdditionOverflow(int left, int right) { if (right < 0 && right != Integer.MIN_VALUE) { return willSubtractionOverflow(left, -right); } else {