I try to compare Integers the next way (for my 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()
and b.intValue()
and recommend me to simplify code to 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 will be compared if a != null
that not best practice as I know. What code should I use?