This question already has an answer here:
- Why does a ternary conditional expression returning null and assigned to a reference type cause a NullPointerException? [duplicate] 2 answers
Can someone explain me why in the first case null pointer was detected, but no on the other ?
Maybe he always looks on the first type, but why he does so only if the condition is false..
@Test
public void test1() {
final Integer a = null;
final Integer b = false ? 0 : a;
//===> NULL POINTER EXCEPTION
}
@Test
public void test2() {
final Integer b = false ? 0 : null;
//===>NOT NULL POINTER EXCEPTION
}
@Test
public void test3() {
final Integer a = null;
final Integer b = true ? 0 : a;
//===>NOT NULL POINTER EXCEPTION
}
@Test
public void test4() {
final Integer a = null;
final Integer b = false ? new Integer(0) : a;
//===> NOT NULL POINTER EXCEPTION
}
@Test
public void test5() {
final Integer a = null;
final Integer b = false ? a : 0;
//===>NOT NULL POINTER EXCEPTION
}