What does the following snippet output?
public class SomeClass {
public static void main(String[] args) {
int a = -10;
int b = -2147483648; // -2147483648 == -2**31
if (Math.abs(a) < -1) {
System.out.println("|a| < -1");
} else {
System.out.println("|a| >= -1");
}
if (Math.abs(b) < -1) {
System.out.println("|b| < -1");
} else {
System.out.println("|b| >= -1");
}
System.out.println("|a| = " + Math.abs(a));
System.out.println("|b| = " + Math.abs(b));
}
}
. . . . . . . . . . . . . . . . . . . . . .
Answer
|a| >= -1
|b| < -1
|a| = 10
|b| = -2147483648
Explanation
Integer values range (in Java) from -2147483648 to 2147483647. This means, the absolute value of -2147483648 is not in the integer range. For more details, see this SO answer.