I'm trying to understand how java deals with ambiguities in function calls. In the following code, the call to method
is ambiguous, but method2
is not!!!.
I feel both are ambiguous, but why does this compile when I comment out the call to method
? Why is method2
not ambiguous as well?
public class A {
public static <K> List<K> method(final K arg, final Object... otherArgs) {
System.out.println("I'm in one");
return new ArrayList<K>();
}
public static <K> List<K> method(final Object... otherArgs) {
System.out.println("I'm in two");
return new ArrayList<K>();
}
public static <K, V> Map<K, V> method2(final K k0, final V v0, final Object... keysAndValues) {
System.out.println("I'm in one");
return new HashMap<K,V> ();
}
public static <K, V> Map<K, V> method2(final Object... keysAndValues) {
System.out.println("I'm in two");
return new HashMap<K,V>();
}
public static void main(String[] args) {
Map<String, Integer> c = A.method2( "ACD", new Integer(4), "DFAD" );
//List<Integer> d = A.method(1, "2", 3 );
}
}
EDIT: This came up in comments: A number of IDEs report both as ambiguous - IntelliJ and Netbeans so far. However, it compiles just fine from command-line/maven.