开发者

Overloading with Short and int

开发者_JAVA百科Why this code will print int?

public static void main(String[] args) {
    short s = 5;
    A(s);
}
public static void A(int a){
    System.out.println("int");
}

public static void A(Short a){
    System.out.println("short");
}


Because upcasting to int was in version 1.0 of Java and auto-boxing was added in version 5.0. Changing the behaviour would break code written for older version of Java.

However, mixing types like this suggests there is something wrong with your design, its only something you are going to find in puzzlers. ;)


Because widening beats boxing

Reason:

Because widening was there long long before where boxing was introduced later on so not to break any code it does this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜