Why does returning null for a primitive work in this case?
The first has a ternary operator which has a result type of Boolean
. The NPE is converting a null
to a boolean
.
It is actually something like:
Boolean temp = s != null ? s.isEmpty() : null; //no problems here
return temp; //crash when temp==null
The second is trying to return a wrong type (Object instead of primitive) - and thus does not compile.