With methods test1()
and test2()
, I get a Type Mismatch Error: Cannot convert from null to int, which is correct; but why am I not getting the same in method test3()
? How does Java evaluate the conditional expression differently in that case? (obviously, a NullPointerException
will rise at runtime). Is it a missing error?
public class Test {
public int test1(int param) {
return null;
}
public int test2(int param) {
if (param > 0)
return param;
return null;
}
public int test3(int param) {
return (param > 0 ? param : null);
}
}
Thanks in advance!