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

前端 未结 6 1539
天涯浪人
天涯浪人 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:25

    This is defined in the JLS #5.2 (Assignment conversion):

    If the expression is a constant expression (§15.28) of type byte, short, char, or int, a narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

    so:

    byte b = 5; //ok: b is a byte and 5 is an int between -128 and 127
    byte b = 1000; //not ok: 1000 is an int but is not representable as a byte (> 127)
    byte b = 5L; //not ok: 5L is a long (and not a byte, short, char or int)
    int i = 5L; //not ok: i is not a byte, short or char
    int i = 5; byte b = i; //not ok: i is not a constant
    final int i = 5; byte b = i; //ok: i is a constant and b is a byte
    

提交回复
热议问题