Wrapper classes - why integer literals fail for Long but work for anything smaller

前端 未结 2 1357
迷失自我
迷失自我 2020-12-10 14:35

Just trying to understand auto-boxing, which I do apart from one thing:

Short s = 250;
Long l = 250;

The assignment to Long l

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 15:08

    Normally, you cannot apply multiple (implicit) conversions in assignment (JLS §5.2 Assignment Conversion):

    Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:

    • an identity conversion (§5.1.1)
    • a widening primitive conversion (§5.1.2)
    • a widening reference conversion (§5.1.5)
    • a boxing conversion (§5.1.7) optionally followed by a widening reference conversion
    • an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.

    Long l=250; requires two conversions (widening primitive conversion followed by boxing conversion), that's why it doesn't compile.

    Long l=250l; compiles because it requires a single boxing conversion.

    But narrowing conversion of a constant expression is a special case, that's why Short s=250; compiles:

    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.
    • A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
      • Byte and the value of the constant expression is representable in the type byte.
      • Short and the value of the constant expression is representable in the type short.
      • Character and the value of the constant expression is representable in the type char.

提交回复
热议问题