Why is “int i = 2147483647 + 1;” OK, but “byte b = 127 + 1;” is not compilable?

后端 未结 4 1025
囚心锁ツ
囚心锁ツ 2020-12-12 18:45

Why is int i = 2147483647 + 1; OK, but byte b = 127 + 1; is not compilable?

4条回答
  •  鱼传尺愫
    2020-12-12 19:27

    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.

提交回复
热议问题