Booleaner, bedingte Operatoren und Autoboxing
Warum wirft dasNullPointerException
public static void main(String[] args) throws Exception {
Boolean b = true ? returnsNull() : false; // NPE on this line.
System.out.println(b);
}
public static Boolean returnsNull() {
return null;
}
während dies nicht
public static void main(String[] args) throws Exception {
Boolean b = true ? null : false;
System.out.println(b); // null
}
?
Die Lösung ist übrigens zu ersetzenfalse
durchBoolean.FALSE
vermeidennull
Unboxed zuboolean
- was nicht möglich ist. Aber das ist nicht die Frage. Die Frage istWaru? Gibt es Referenzen in JLS, die dieses Verhalten bestätigen, insbesondere im 2. Fall?