Consider the following code:
int x1 = 0b1111_1111_1111_1111_1111_1111_1111_111; // binary for 2147483647
System.out.println(x1); // prints 2147483647
int x2 = 2147483648; // The literal 2147483648 of type int is out of range
// x3 is binary representation for 2147483648
int x3 = 0b1000_0000_0000_0000_0000_0000_0000_0000; // accepted without any compile errors
System.out.println(x3); // prints -2147483648
// x4 is binary representation for 4294967295
int x4 = 0b1111_1111_1111_1111_1111_1111_1111_1111; // long value: 4294967295
System.out.println(x4); // prints -1
int x5 = 0b1111_1111_1111_1111_1111_1111_1111_1111_1; // The literal 0b1111_1111_1111_1111_1111_1111_1111_1111_1 of type int is out of range
The Integer.MAX_VALUE
is 2147483647
and compiler accepts any int
in that range, and throws an error when this value goes beyond 2147483647
. However, the int x3
(int: -1, long: 2147483648) and x4
(int: -1, long: 4294967295) in above snippet is accepted without any errors but throws errors in case of x5
.
First question: Why the compiler did not complain about the range of x3
?
Second question: If the value of x3
and x4
is accepted without any errors, why does it throw errors in case of x5
?