77

The following code compiles (with Java 8):

Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);

But what does it do?

Unbox i1:

boolean compared = (i1.intvalue() == i2);

or box i2:

boolean compared = (i1 == new Integer(i2));

So does it compare two Integer objects (by reference) or two int variables by value?

Note that for some numbers the reference comparison will yield the correct result because the Integer class maintains an internal cache of values between -128 to 127 (see also the comment by TheLostMind). This is why I used 1000 in my example and why I specifically ask about the unboxing/boxing and not about the result of the comparison.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Thirler
  • 20,239
  • 14
  • 63
  • 92
  • 11
    Surely if you ran this code and printed the output you could determine this yourself. – Scruffy May 26 '15 at 09:28
  • The question isn't exactly same as the one used to marked it as duplicate :) – TheLostMind May 26 '15 at 09:32
  • Check this [question - auto boxing rules](http://stackoverflow.com/questions/12559634/java-autoboxing-rules). In its answer, clearly, this case has been covered. – Pham Trung May 26 '15 at 09:33
  • I don't think this question is a duplicate of the one marked. This question asks how the compiler decides whether to box or unbox when comparing a primitive with a wrapper class object. +1 – Chetan Kinger May 26 '15 at 09:33
  • 9
    @Scruffy - Its not that simple. `Integer` class maintains an internal cache of values between `-128 to 127`. So even if you compared `Integer i1=100` with `Integer i2=100` using `==` , you will get `true`. You will get `false` when both `i1` and `i2` are not in that local cache range – TheLostMind May 26 '15 at 09:43
  • 2
    @TheLostMind, good remark. This is why I picked 1000 as my example, I will add this to the question. – Thirler May 26 '15 at 09:44
  • @Thirler - The question looks better now :) – TheLostMind May 26 '15 at 09:48
  • 1
    somewhat related: http://stackoverflow.com/a/30415260/719662 –  May 26 '15 at 14:03

3 Answers3

78

It is defined in the JLS #15.21.1:

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).

And JLS #5.6.2:

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:

  • If any operand is of a reference type, it is subjected to unboxing conversion [...]

So to answer your question, the Integer is unboxed into an int.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
46

Lets do some examples :

Case -1 :

       public static void main(String[] args) {
            Integer i1 = 1000;
            int i2 = 1000;
            boolean compared = (i1 == i2);
            System.out.println(compared);
        }

Byte code :

....
        16: if_icmpne     23 // comparing 2 integers
....

Case -2 :

public static void main(String[] args) {
    Integer i1 = 1000;
    Integer i2 = 1000;
    //int i2 = 1000;
    boolean compared = (i1 == i2);
    System.out.println(compared);
}

Bytecode :

...
     16: if_acmpne     23 // comparing references
....

So, in case of comparison of Integer and int with == the Integer is unboxed to an int and then comparison happens.

In case of comparing 2 Integers, the references of 2 Integers are compared.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
5

Explanation

  1. When two primitive values are compared using == operator autoboxing does not take place.

  2. When two objects are compared using == operator autoboxing plays role.

  3. When mixed combination is used that is it contains an Object and primitive type and comparison is done using == operator unboxing happens on the Object and is converted to primitive type.

Please go through the below link which will help you get understand detailed about auto-boxing with suitable example.

Refer Link : http://javarevisited.blogspot.in/2012/07/auto-boxing-and-unboxing-in-java-be.html

karan shah
  • 77
  • 5