The error happens because in your case the standard requires unboxing the value of a boxed type:
If one of the second and third operands is of primitive type T
, and the type of the other is the result of applying boxing conversion (§5.1.7) to T
, then the type of the conditional expression is T
.
In your case T
is long
, because 10L
is long
and lNull
is Long
, i.e. the result of applying boxing conversion to long
.
The standard further says that
If necessary, unboxing conversion is performed on the result.
This is what causes the exception. Note that if you invert the condition, the exception would no longer be thrown:
Long res = obj != null ? lNull : 10L;
You can fix the problem by explicitly asking for Long
instead of using long
, i.e.
Long res = obj == null ? lNull : Long.valueOf(10L);