In the second case, the compiler can infer that the null
must be a Long
type. In the first case, it doesn't - and assumes that the expression returns a long
. You can see that this is the case (i.e. fix it) like,
Long res = obj == null ? lNull : (Long) 10L;
which does not yield a NullPointerException.