public class Test { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
short y= 6;
go(y);
}
public static void go(Short n){
System.out.println("Short");
}

public static void go(int n){
System.out.println("int");
}

}运行下结果是 int
也就是,当short参数同时适合2个方法时候,会自动选择下面一个(int)类型的,而不是包装器作为参数的类型的,我不怎么懂,有人能讲解下么?

解决方案 »

  1.   

    首先会考虑拓宽基本类型来进行匹配。
    Method invocation 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 widening reference conversionan unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.If, after the conversions listed above have been applied, the resulting type is a raw type (§4.8), an unchecked conversion (§5.1.9) may then be applied. It is a compile time error if the chain of conversions contains two parameterized types that are not not in the subtype relation.
    可以自己读一下java语言规范。
      

  2.   

    搂主注意到自己的那个方法定义的问题吗。
    不知道你是不是不小心写错。
    public static void go(Short n)
    大写的Short和short完全是两码事。下面代码跑出来的是short.
    import java.util.ArrayList;public class XDR_Test { public static void main(String[] args) {
    short s = 8;
    go(s); }

    private static void go(short i) {
    System.out.println("short");
    }

    private static void go(int i) {
    System.out.println("int");
    }
    }
      

  3.   

    很明显lz的short写成了Short撒。。