Short to int automatic promotion

后端 未结 2 1449
借酒劲吻你
借酒劲吻你 2021-01-13 21:26
public class Yikes1 {
    public static void go(Long n) {
        System.out.println(\"Long \"); // printed
    }

    public static void go(Short n) {
        Syste         


        
相关标签:
2条回答
  • 2021-01-13 21:58

    The binding sequence works as follows:

    1. Exact match (Ex. int--> int)
    2. Promotion (Ex. int --> long)
    3. Autoboxing/Unboxing (Ex. int --> Integer)
    4. Varags (Ex. int --> int...)
    0 讨论(0)
  • 2021-01-13 22:04

    Okay, when there's no method, which accepts short, there's 2 options: autobox it into Short or cast to integer. JLS states, that the second option in prefered:

    Method invocation contexts allow the use of one of the following:

    1. an identity conversion (§5.1.1)

    2. a widening primitive conversion (§5.1.2)

    3. a widening reference conversion (§5.1.5)

    4. a boxing conversion (§5.1.7) optionally followed by widening reference conversion

    5. an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.

    What you expect here is a boxing conversion, but what you get is a widening primitive conversion.

    You can read more about boxing here to correctly understand the relation between short and Short.

    0 讨论(0)
提交回复
热议问题