Suppose, I have a simple method:
public int add(int a, int b) {
return a + b;
}
If I run it with max int values
add(2147483647, 2147483647);
I get -2. I found that if we overflow int then we go to min integer value (-2147483648) and keep adding.
Let's do some math. If I add 2147483647 + 2147483647 I should get -1, because
-2147483648 + 2147483647 = -1
So why do I get -2?