Why is assigning 'int constant -> byte variable' valid, but 'long constant -> int variable' is not?

前端 未结 6 1540
天涯浪人
天涯浪人 2021-01-19 06:23

I have this code snippet:

int i = 5l; // not valid (compile error)
byte b = 5; // valid

What do you think about it?

Why?

6条回答
  •  耶瑟儿~
    2021-01-19 07:12

    One thing that comes to my mind right now why int i = 5l; is not possible, because the range of long is bigger than int range and thus putting long value in variable that of int could not be possible.

    int range: -2147483648… 2147483648, and long range: -2^63… 2^63-1

提交回复
热议问题