java - Comparing two Integers 'Unnecessary unboxing' warning -
i try compare integers next way (for case, it's good):
public void comparemaynull(integer a, integer b) { if ((a == null ? -1 : a.intvalue()) == b.intvalue()) system.out.println("true"); }
intellij idea gives me warning 'unnecessary unboxing' on a.intvalue()
, b.intvalue()
, recommend me simplify code this:
public void comparemaynull(integer a, integer b) { if ((a == null ? -1 : a) == b) system.out.println("true"); }
but i'm little confused, because references compared if a != null
not best practice know. code should use?
because have -1
literal int
type, a
auto-unboxed, (if , if a == null
false
else expression not evaluated) in evaluation of ternary conditional
a == null ? -1 : a;
then since expression of type int
, b
auto-unboxed.
therefore can write if ((a == null ? -1 : a) == b)
, safe in knowledge java auto-unbox both a
, b
. explicit unboxing via calls intvalue()
superfluous , ide warning of this. avoidance of doubt a
not auto-unboxed when expression a == null
evaluated.
personally find 1 of pernicious parts of java. i'd consider writing way had it, , tell ide suck it.
Comments
Post a Comment