Estoy tratando de comprender cómo Java maneja las ambigüedades en las llamadas a funciones. En el siguiente código, la llamada a method
es ambigua, pero method2
no es !!!.Métodos java sobrecargados ambiguos con genéricos y varargs
Siento que ambos son ambiguos, pero ¿por qué compila esto al comentar la llamada al method
? ¿Por qué el method2
no es ambiguo también?
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: Este surgió en los comentarios: Un número de entornos de desarrollo informan tanto como ambigua - IntelliJ y Netbeans hasta ahora. Sin embargo, compila muy bien desde la línea de comandos/maven.
Buena pregunta ... – Jivings
tal vez sería aumentar el tiempo de compilación si java comprueba métodos utilizados – Joelmob
@Joelmob siento la pregunta es ¿por qué 'method2' compilar cuando parece ser tan ambiguo como 'método'. – Jivings