If wrapped primitive values (e.g. Integers
and Floats
) are used in a ternary operator (e.g. a?b:c
), both
values will be unboxed and coerced to a common type, potentially leading to unexpected results. To avoid this, add an explicit cast to a compatible
type.
Integer i = 123456789; Float f = 1.0f; Number n = condition ? i : f; // Noncompliant; i is coerced to float. n = 1.23456792E8
Integer i = 123456789; Float f = 1.0f; Number n = condition ? (Number) i : f; // n = 123456789