public void m1(Integer f) {
...
}
public void m1(Float f) {
...
}
public void main() {
m1(null); // error: the method m1(Integer) is ambiguous for the type Main
m1((Integer) null); // success
}
dado el ejemplo anterior, se puede admitir de alguna manera que null
se escribe. Entonces, ¿por qué las siguientes líneas imprimen true
? Claro o1
y o2
ambos no tienen ningún valor (es decir, null
), pero no son del mismo tipo (Integer
vs Float
). Primero pensé que false
habría sido impreso.Comparación de dos objetos nulos a partir de dos tipos diferentes
Integer i = null;
Object o1 = (Object) i;
Float f = null;
Object o2 = (Object) f;
System.out.println(o1 == o2); // prints true
// in short:
System.out.println(((Object) ((Integer) null)) == ((Object) ((Float) null))); // prints true
Null no tiene un tipo: [http : //stackoverflow.com/questions/2707322/what-is-null-in-java] [1] [1]: http://stackoverflow.com/questions/2707322/what-is-null-in -java – Stegrex
'null' está escrito; ver mi respuesta – irreputable