0

This is not a duplicate question! I'm not comparing two Integers, but an Integer and an int value. Moreover, I'm asking for what people think about what I'm doing not to tell me how to properly compare...

I just want to know what do you think about comparing an object of type Integer with an int value like this :

final Integer i = null; //I'm setting it to null just to show that my test will not throw a NullPointerException, otherwise I could used a simple `int`
if (i == Integer.valueOf(3))
    ...

What I found useful with this is that there is no need to check if my object is null or not. Moreover Integer.valueOf() caches Objects....

frontGeek
  • 43
  • 8

3 Answers3

2

That is a bad idea as it can give you false results.

final Integer i = Integer.valueOf(Integer.MAX_VALUE);
System.out.println(i == Integer.valueOf(Integer.MAX_VALUE));

gives you false

I guess you tried some small numbers and they gave you the correct result. But once the number you try is above 127 or below -128 your result will most likely be wrong

public static Integer valueOf(int i)

[...] This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

Source

Community
  • 1
  • 1
Pinguin895
  • 999
  • 2
  • 11
  • 28
1

IMHO it does not make the code easier to read, makes it more error-prone (as not all values are cached), and it is not even faster than the "traditional" approach. You do yourself, and your fellow developers a favor, if you use the good old equals().

Anyway, I think that nobody should ever use == with objects, if they mean value equality. The only exception from this rule are enums.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • I see, but if use `equals` I'll need to check if my object if null or not before ... – frontGeek Sep 28 '15 at 13:21
  • 3
    @frontGeek It depends on from what side you start your `equals`. If you do it like this `Integer.valueOf(3).equals(i)` you wouldn't need a `null` check. – Sergey Kalinichenko Sep 28 '15 at 13:22
  • 1
    @frontGeek That depends on where the object comes from. If it's from code that you write yourself then just make sure you never return a `null` object in the first place. – JonK Sep 28 '15 at 13:22
  • 1
    Or, you can use `new Integer(3).equals(variable)` – meskobalazs Sep 28 '15 at 13:23
  • 1
    Or you just trash the idea of using these boxed comparisons in the first place and do `i == 3` like a normal person? Setting a primitive wrapper object to `null` seems like a *really* bad idea anyway. – Clashsoft Sep 28 '15 at 13:26
  • @Clashsoft normally, normal person needs to check if the object is null before doing `i == 3` ... however I'm not setting that object to null, I just did that to show that even if my object is null, the comparison will not throw a NullpointerException – frontGeek Sep 28 '15 at 13:29
  • @frontGeek What is your reason for using `Integer` rather than `int`? – Paul Boddington Sep 28 '15 at 13:31
  • I've an object to handle it's not my choice to use int instead ... – frontGeek Sep 28 '15 at 13:33
  • 1
    OK, well the solution is to use `Integer.valueOf(3).equals`. IMHO you should not rely on the fact that values between -128 and 127 are cached. Even though they are, you're asking for trouble later. – Paul Boddington Sep 28 '15 at 13:35
1

Check Integer.valueOf java docs: This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

May cache or may not, so your code will not work in that case.

Vovka
  • 599
  • 3
  • 10