这个代码看看:SCJP问题:方法暧昧
public class Test {
public static void main(String... args) {
flipFlop("hello", new Integer(4), 2004);
// flipFlop("hello", 10, 2004); // this works!
}
private static void flipFlop(String str, int i, Integer iRef) {
System.out.println(str + " (String, int, Integer)");
}
private static void flipFlop(String str, int i, int j) {
System.out.println(str + " (String, int, int)");
}
}
编译器给出错误的调用不明确:
描述资源路径位置类型 方法触发器(字符串,int,Integer)对于Test Test.java类型是不明确的scjp19 - inheritence/src line 3 Java问题
但是,如果commente d-out行用于调用触发器,该方法明确地被调用(第二个,因为在使用原语本身后出现自动装箱)。
我希望编译器能够看到第二个参数将以某种方式拆箱,并根据第三个参数判断必须调用哪个方法。为什么不发生这种情况?基本原理是什么?
Dupe:http://stackoverflow.com/questions/501412/why-does-autoboxing-make-some-calls-ambiguous-in-java – BalusC 2010-03-07 17:13:28