0

This is classical, but writing to Google did not give me hits.

My code:

Integer i = ..; // Sth is put from database.
if (i != 1) {
   // do sth
} else {
   // do not.
}

Case:

I know that this comparison is not correct java and I should compare:

if (i.intValue != 1) {}

or

if(!i.equals(1)) {}

but my code had the first one and I seem to get the true from somewhere, where the Integer is not 1 and when it is 1 there comes false.

Question:

What is happening there around?

mico
  • 12,730
  • 12
  • 59
  • 99
  • 4
    One word, autoboxing... `Integer` will be automatically converted to `int` when needed. – M. Deinum Sep 17 '14 at 12:46
  • 1
    Integers are autounboxed – Matias Cicero Sep 17 '14 at 12:46
  • Please be aware, however, that autounboxing does not come in play in some cases when doing a `reference comparison`. See this question for more info: [Comparing Integer Values in Java, Strange Behavior](http://stackoverflow.com/questions/10002037/comparing-integer-values-in-java-strange-behavior) – Matias Cicero Sep 17 '14 at 12:49

4 Answers4

3

but my code had the first one and I seem to get the true from somewhere, where the Integer is not 1 and when it is 1 there comes false.

If I understand your issue correctly, the following might explain the behavior:

Integer i = 1;
Integer j = new Integer(1);
Integer k = Integer.valueOf(1);

System.out.println(i == j);  // false
System.out.println(i == k);  // true

In other words, you can get both true or false when comparing with 1 depending on how the Integer was constructed. Integer.valueOf will reuse objects while new Integer will not.

If you indeed did the comparison with an integer literal (or with an int) then any Integer should be automatically unboxed by the compiler, and you should never get any surprises.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 1
    Sth like this may be there, because this part of code was working just opposite to my common sense and thus I had to ask sillies. – mico Sep 17 '14 at 12:56
  • Yeah. Autoboxing has bitten me many times. (For instance when trying to look up a value in a `Map` while accidentally providing a `Long` as key, thinking it would convert it to an integer.) – aioobe Sep 17 '14 at 12:59
  • I also remembered some notation that == with two Integer somehow compare the object, not value. Like pointed in your and other answers, this is not the case always, though. – mico Sep 17 '14 at 16:54
1

The compiler changes :

if (i != 1)

to

if(i.intValue()!=1)
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

See this link

Miguel Prz
  • 13,718
  • 29
  • 42
1

if (i != 1) , if (i.intValue != 1) and if(!i.equals(1)) are all equal and will return the same value. The compiler will automatically unbox i, turning it into a primitive int.

ortis
  • 2,203
  • 2
  • 15
  • 18