Ricevo NullPointerException in un'istanza di seguito mentre la sua controparte è scorrevole.Perché l'operazione ternaria fornisce un punto nullo mentre la sua controparte ifelse no?
public static void main(String[] args){
System.out.println(withTernary(null, null)); //Null Pointer
System.out.println(withIfElse(null, null)); //No Exception
}
private static Boolean withTernary(String val, Boolean defVal){
return val == null ? defVal : "true".equalsIgnoreCase(val);
}
private static Boolean withIfElse(String val, Boolean defVal){
if (val == null) return defVal;
else return "true".equalsIgnoreCase(val);
}
Online version with the lines in main
reversed, che emette null
da withIfElse
e poi fallisce in withTernary
.
Sto usando seguente versione java
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
Questo è un duplicato. – Raedwald
@ Raedwald: di cosa? Perché non contrassegnarlo come tale? –
Questo è veramente ... dannatamente confuso. Questo è un caso molto particolare nel JLS ... – nneonneo