看看这段代码:
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)");
}
}
编译器给出调用不明确的错误:
描述资源路径位置类型方法flipFlop(String, int, Integer) 对于Test Test.java scjp19-inheritence/src line 3 Java 问题类型不明确
但是,如果注释掉的行用于调用触发器,则该方法将被明确调用(第二个,因为自动装箱是在使用原语本身之后进行的)。
我希望编译器看到第二个参数将以一种或另一种方式拆箱,并根据第三个参数判断必须调用什么方法。为什么不会发生这种情况?理由是什么?