public static void test(double a){
System.out.println("double: "+a);
}
public static void test(long a){
System.out.println("long : "+a);
} public static void test(Integer a){
System.out.println("Integer: "+a);
}
在main中,我调用 test(2);请问各位 这里应该执行哪一个方法?说明原因。不要去运行了再来说答案,呵呵,各位可以自检一下基础如何。

解决方案 »

  1.   

    肯定是long了,首先排除参数为double的方法参数为Integer的方法需要一个Integer类的对象,2并不是所以应该是执行参数为long的方法个人见解
      

  2.   

    public static void test(long a){ 
    System.out.println("long : "+a); 

      

  3.   

    明显是long
    没int 没long 就Integer先widden->再向上转型->最后比较args
      

  4.   

    想问各位的是要是这里没有public static void test(long a){ 
    System.out.println("long : "+a); 
    }  呢???应该是执行哪一个?
      

  5.   

    int,float,double
    不会调用Integer
      

  6.   

    没有long,支持double
    Integer要经过装箱操作
      

  7.   


    高级重载优先级
    1.widden
    2.boxing
    3.var-args
      

  8.   

    离转换类型最近的:long,如果long和double都没有,而有Integer则调用这个,1.5中自动装箱