Let’s have a simple code as below
public class Sandbox {
public static void run(String[] args) {
int i = Integer.MAX_VALUE;
int j = i+1;
System.out.println(i);
System.out.println(j);
}
}
This will print output
2147483647
-2147483648
Value at j is overlowed without any notice to me when the code is executed.
I use Netbeans IDE to run this code.
Is it possible to configure the system to notify us when such arithmetic overflow occurs?
See the last sentence in the Java Language Specification (Chapter 15.18.2. Additive Operators (+ and -) for Numeric Types) :
For a lengthy article on this problem and the presentation of a tool for workaround see the article Signalling Integer Overflows in Java.