Why is int i = 2147483647 + 1; OK, but byte b = 127 + 1; is not compilable?
int i = 2147483647 + 1;
byte b = 127 + 1;
Constants are evaluated as ints, so 2147483647 + 1 overflows and gives you a new int, which is assignable to int, while 127 + 1 also evaluated as int equals to 128, and it is not assignable to byte.
2147483647 + 1
int
127 + 1
128
byte
2.1m questions
2.1m answers
60 comments
57.0k users