The problem is not related to boxing or unboxing. What happens is that the ternary operator (? :) has a somewhat peculiar behavior. For example, in the following code:
a.equals(1) ? a : b
a.equals(1)
corresponds to the conditional expression which determines what varores (in this case a
or b
) will "return" as a result. If the expression to the left of ?
is evaluated as true
then the value resulting from evaluating the expression will be returned to the left of the colon (:); and if evaluated as false
, the value resulting from evaluating the expression to the right of the two points is returned. Now, it is recommended that the data types that return the evaluation of the expressions to the left and right of the two points be the same, to avoid errors and / or strange behaviors like in this case.
What is happening to you, that a
is type Integer
and b
% type Double
, so the compiler determines that the type of data that returns as a result of the statement a.equals(1) ? a : b
will be Double
, since you can not convert a Double
to Integer
because data would be lost (at first because the decimal part is lost), but on the other hand if you can convert a Integer
to Double
without being lose data.
For all the above I reiterate the recommendation: "try that the expressions to the left and to the right of the two points (:) are evaluated to the same type of data".