Overloading with Short and int

前端 未结 2 1590
有刺的猬
有刺的猬 2020-12-11 02:30

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         


        
相关标签:
2条回答
  • 2020-12-11 02:50

    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 讨论(0)
  • 2020-12-11 02:53

    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. ;)

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