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

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

    Language specification allows that (note that 5, 127 or 128 is an integer literal):

        byte b = 127; 
    

    this will generate error:

        byte b = 128;
    

    this is called implicit narrowing primitive conversion, and is allowed by JLS:

    In addition, 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.

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

    so, below is compiler error because of above statement

    int i = 5l;
    

    5l is long and not constant expression (§15.28) of type byte, short, char, or int. Also it fails to be correct becasue it is an int, bacause if the type of the variable is byte, short, or char.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-19 07:19

    as according to my understanding,

    Because you have to cast the type long to int , for the first option , i.e

    int i = (int) 5l;
    

    and in second case, as the value assignment to byte is same like assignment to int,

    you think i am assigning int value to byte here

    byte b= 5; ????
    

    no its not, its getting the value as byte itself, as byte have a range from -128 to 127,

    for further detail see HERE

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-19 07:25

    In first case

    int i = 5l;  // trying to allocate 64bits in 32 bits space. Err
    

    where as

    byte b = 5; //  byte can represented in a  range is -128 to 127. Compiles fine
    
    0 讨论(0)
  • 2021-01-19 07:27

    Just assuming here because there is unlikely to be a definitive answer.

    For

    int i = 5l;
    

    the compiler assumes there is a good reason you wrote 5l and not 5 and so it is an error.

    For

    byte b = 5;
    

    there is no byte literal way of writing 5 and so it would be needlessly pedantic to insist you write (byte) 5 every time and in fact it would be error prone.

    byte b = 222;        // is an error
    byte b = (byte) 222; // is NOT an error
    
    0 讨论(0)
提交回复
热议问题