Wiem, że to stary post, ale wygląda na bardzo podobnej sprawie, a następnie głosować mnie: P
Odpowiadając na oryginalne pytanie: tylko jeden operand jest oceniany ALE:
@Test
public void test()
{
Integer A = null;
Integer B = null;
Integer chosenInteger = A != null ? A.intValue() : B;
}
Ten test zawsze będzie rzutował NullPointerException
iw tym przypadku statystyki IF nie są równoważne operatorowi?:.
Powód jest tutaj http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.25. Część o boks/unboxing jest uwikłany, ale może być łatwo zrozumiałe patrząc na:
"If one of the second and third operands is of type boolean
and the type of the other is of type Boolean
, then the type of the conditional expression is boolean
."
To samo odnosi się do Integer.intValue()
Pozdrawiam!
Jest to również dość proste, że można po prostu wypróbować i zobaczyć, co się stanie :) –
Co daje informacje na temat jednej konkretnej instancji. Lepiej znaleźć to, co mówi standard. –