這個代碼看看: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