看看这段代码:
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)");
}
}
Run Code Online (Sandbox Code Playgroud)
编译器给出一个错误,调用是不明确的:
说明资源路径位置类型方法flipFlop(String,int,Integer)对于Test Test.java scjp19类型是不明确的 - inheritence/src第3行Java问题
但是如果使用注释掉的行ti调用触发器,则该方法被明确地调用(第二个,因为在使用基元本身之后进行自动装箱).
我希望编译器看到第二个参数将以某种方式取消装箱,并根据第三个参数判断必须调用哪个方法.为什么不这样呢?理由是什么?