Missing Java error on conditional expression?
- by Federico Cristina
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 evaluates the conditional expression differently in that case? (obviusly, a NullPointerException will rise in 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 ? return param : return null);
}
}
Thanks in advance!