Below is the code snippet , where I am confused .The return type of method is int , so the method can not return null.But , when I uncomment 1st return statement and comment 2nd return statement, method’s doesn’t show any error not even warning.
I am confused what is the reason behind that.I am using java version 7.
protected int calculateLogicalPageRangeForTitles(String logicalpage) throws Exception {
//return StringUtils.isNumeric(logicalpage) ? Integer.parseInt(logicalpage) : null;
return null;
}
The code is legal and compiles.
See Java Spec:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25
This means, the result of the ?: will be an Integer, which will be unboxed to int. This unboxing will throw a NullPointerException.