Missing Java error on conditional expression?

Posted by Federico Cristina on Stack Overflow See other posts from Stack Overflow or by Federico Cristina
Published on 2010-06-07T02:37:23Z Indexed on 2010/06/07 2:42 UTC
Read the original article Hit count: 514

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about convert