What is the cleanest way to compare an int with a potentially null Integer in Java?
Asked Answered
L

6

11
Map<String,Integer> m;
m = new TreeMap<String,Integer>();

Is it good practice to add the following cast just to avoid the null pointer exception when m.get() is null.

System.out.println( ((Integer) 8).equals( m.get("null") ) ); // returns false

Alternatively with a prior null check it starts to look a bit ugly.

System.out.println( m.contains("null") != null && m.get("null").equals( 8 ) );

Is there a better way to write this? Thanks.

Lashandralashar answered 8/10, 2014 at 13:57 Comment(1)
It depends on who else is coming to the cotillion.Robenarobenia
H
10

I try to avoid casts whenever possible, so I'd rather use the following, which also looks nicer in my opinion:

Integer.valueOf(8).equals(m.get("null"))
Halliburton answered 8/10, 2014 at 14:3 Comment(3)
Of course they end up generating essentially the same code. The "cast" is really an auto-boxing directive.Robenarobenia
@HotLicks It is, and casting a primitive to its respective boxing type is even statically type checked. I still don't like it for stylistic reasons. The more severe problem with the OP's code is of course using == to compare Integers for equality as other answers have pointed out. I must admit that I have overlooked that at a first glance, answering only his immediate question whether the cast “is considered poor style” (question edited since then).Halliburton
Yeah, I guess he was originally using ==, but edited that away pretty quickly. The odd thing is that it would actually work, for small value positive ints, since they are essentially "interned".Robenarobenia
C
9

The == operator doesn't compare values, but references.

You should use the .equals() method, instead, applied to the Integer variable (for which you are sure that is not null and NPE won't be thrown):

Integer eight = 8; //autoboxed
System.out.println(eight.equals(m.get("null")));

This will print false even the m.get("null") returns null.

Catlike answered 8/10, 2014 at 14:2 Comment(0)
G
8

No, because it will not work. You can't compare two Integer with ==, as that compares references and not the integer values. See more info in this question

You'll need a helper method, such as:

boolean safeIntegerCompare(Integer a, int b)
{
    if (a != null) 
      return a.intValue() == b;
    return false;
}
Gritty answered 8/10, 2014 at 14:3 Comment(1)
I don't believe he was intending to do that (though I'll admit that his full intent is hard to divine).Robenarobenia
M
1

If only one of the arguments may be null (as is the case when you're comparing an unknown value to a constant), use equals() like this:

Integer foo = Integer.valueOf(8); // you could write Integer foo = 8; here too but I prefer to avoid autoboxing
if (foo.equals(m.get("x"))) { //will never throw an NPE because foo is never null
   ...
}

Note that your example isn't going to work in general because comparing non-primitive values with == only returns true if they refer to the same object instance. (Which in this case might even be true for very specific reasons but most of the time it isn't.)

Milliemillieme answered 8/10, 2014 at 14:2 Comment(3)
This is essentially what the OP proposed.Robenarobenia
@HotLicks It wasn't when I wrote my answer, OP edited question since.Milliemillieme
Yeah, I agree the question was vaguely worded from the start and he edited it about a dozen times. The topic has become pretty muddled.Robenarobenia
A
1

To expand the accepted answer: i find myself having to check the equality of 2 Integer variables which might or might not be null.

So my solution would be:

boolean equals = Optional.ofNullable(objectA.getNullableInteger()).equals(Optional.ofNullable(objectB.getNullableInteger());

Aryl answered 28/12, 2020 at 9:13 Comment(1)
Wouldn't it be easier to just use Objects.equals here? boolean equals = Objects.equals(objectA.getNullableInteger(), objectB.getNullableInteger());Dysphemia
D
1

You can use Objects.equals

int foo = 1;
Integer bar = null;
Objects.equals(foo, bar);
Duumvir answered 19/10, 2022 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.