Take a look at this code:
public class Test {
public static void main(String... args) {
flipFlop("hello", new Integer(4), 2004);
}
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)");
}
}
The compiler generates an error that causes ambiguity:
Description Resource path location type The flipFlop (String, int, Integer) method is ambiguous for the type Test Test.java scjp19 - inheritence / src line 3 Java problem
But if a commented line ti is used, it calls a flip flop, the method is uniquely called (the second, since autoboxing comes after using the primitive itself).
I would expect the compiler to see that the second argument will be unpacked anyway, and judge which method should be called depending on the third argument. Why is this not happening? What is the reason?
source
share