import static java.lang.Math.addExact;
import static java.lang.Math.subtractExact;
public class OverflowCheck {
public static boolean addingWillOverflow(Integer x, Integer y) {
try {
addExact(x, y);
subtractExact(x, y);
return false;
} catch (ArithmeticException e) {
return true;
}
}
public static void main(String[] args) {
Integer x = 39839299, y = 1472783642;
System.out.println(addingWillOverflow(x, y) ? "true" : "false");
x = 1338839299; y = 1872783642;
System.out.println(addingWillOverflow(x, y) ? "true" : "false");
}
}
/*
run:
false
true
*/