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

后端 未结 4 1011
囚心锁ツ
囚心锁ツ 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:08

    As an evidence to @MByD:

    The following code compiles:

    byte c = (byte)(127 + 1);
    

    Because although expression (127 + 1) is int and beyond the scope off byte type the result is casted to byte. This expression produces -128.

提交回复
热议问题