3
Integer a = 5;
int b = 5;

System.out.println(a==b); // Print true

But why does this print true, since a is an instance of an Integer and b is a primitive int?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 1
    This feature is more than 10 years old, so it is surprising that this doesn't appear anywhere on the internet ;) – Peter Lawrey Jan 01 '16 at 08:04
  • Possible duplicate of [Why Java does not see that Integers are equal?](http://stackoverflow.com/questions/4428774/why-java-does-not-see-that-integers-are-equal) – Aaditya Gavandalkar Jan 01 '16 at 08:31
  • Here is a very interesting question http://stackoverflow.com/questions/15024933/why-equal-operator-works-for-integer-value-until-128-number – awsome Jan 01 '16 at 08:33
  • @AadityaGavandalkar: No, that's definitely *not* a duplicate of *that* question. – Makoto Jan 01 '16 at 08:33
  • @awsome: It's interesting, but it's unrelated to this scenario. – Makoto Jan 01 '16 at 08:34
  • that is right but just to make you aware of something interesting about Integer equality – awsome Jan 01 '16 at 08:35

4 Answers4

15

Java uses the concept of Unboxing when you compare primitive and wrapper class. Where in a Integer variable is translated into primitive int type.

Following is what is happening with your code:

Integer a = 5; //a of type Integer i.e. wrapper class
int b = 5; //b of primitive int type

System.out.println(a==b) // a is unboxed to int type and compared with b, hence true

For more on Autoboxing(reverse of Unboxing) and Unboxing this link.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
4

The correct answer is due to unboxing, but let's be more explicit here.

The rules for numerical equivalence are described in the Java Language Specification, specifically these rules:

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).

Since Integer is convertible to a numeric type (per these rules), the values you're comparing become semantically equivalent to a.intValue() == b.

It should be stressed that this conversion will fail if a is null; that is, you will get a NullPointerException when attempting to do that equivalence check.

Makoto
  • 104,088
  • 27
  • 192
  • 230
3

Beginning with JDK 5, Java added two important features: autoboxing and auto-unboxing.

Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly construct an object.

Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed.

With autoboxing, it is no longer necessary to manually construct an object in order to wrap a primitive type:

Integer someInt = 100; // autobox the int (i.e. 100) into an Integer

To unbox an object, simply assign that object reference to a primitive-type variable:

int unboxed = someInt // auto-unbox Integer instance to an int
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
1

Here all the post answer are correct.

Here your code at Compile time

public class Compare {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Integer a=5;
        int b=5;

        System.out.println(a==b);
        //the compiler converts the code to the following at runtime: 
        //So that you get it at run time
        System.out.println(a==Integer.valueOf(b));


    }

}

amit prasad
  • 574
  • 3
  • 15