The differences are due to the ternary operator behaviour in Java.
The ternary conditional case:
In the expression late ? lateTime : time
, Java will auto-unbox exactly one of the arguments (according to the value of late
) to its respective primitive type. (You can observe this by setting time
to null
and late
to true
: a NullPointerException
is not thrown. The same applies when setting lastTime
to null
and late
to false
.)
If the value of the expression will be time
, then this is widened to a double
.
In either case, the resulting double
is auto-boxed to a Double
in assigning it to result
.
The simple assignment case:
In writing Double result = time;
, Java disallows this as it expects you to be more explicit.
Personally I find the mechanism of the Java ternary conditional operator with respect to the boxed primitive types to be one of the most pernicious parts of the language.