-1
public class Application {
    public static void main(String[] args) {
        Integer a = new Integer(10);
        Integer b = new Integer(10);
        int x = new Integer(10);
        int y = new Integer(10);
        int p = 10;
        int q = 10;

        System.out.println(a == b);
        System.out.println(a == x);
        System.out.println(x == p);
        System.out.println(b == q);
        System.out.println(y == q);
        System.out.println(x == y);

    }
}

The above code produces the output:

false
true
true
true
true
true
  1. Please explain the process of primitive type getting compared to a reference type using == ?
  2. how int x = new Integer(10); getting evaluated internally?
Turing85
  • 18,217
  • 7
  • 33
  • 58
Eddy
  • 19
  • 2
  • You might be interested in comparing a primitive type with the value of Object but not the object. – joey rohan Jul 22 '15 at 20:39
  • https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html – Cratylus Jul 22 '15 at 20:41
  • Exactly sir, my aim is to get my concept cleared on how primitive types are getting compared with the value of object and also about what kind of value "b" hold and how unboxing is working when "b" gets compared to "q". – Eddy Jul 22 '15 at 20:43
  • You might want to study [this](https://docs.oracle.com/javase/tutorial/). Uppermost block called "Trails covering the basics". – Turing85 Jul 22 '15 at 20:49
  • Why do you create the same question twice? Wasn't the provided duplicate link clear in its answers? – Tom Jul 22 '15 at 21:07
  • no, It was not clear in the previous post @Tom but here it really became clear – Eddy Jul 22 '15 at 21:24
  • Then you should edit your question to explain why it either isn't a duplicate or what is still unclear. Just a note for "next time". – Tom Jul 22 '15 at 21:28

3 Answers3

1

When you compare the primitive int with its wrapper class Integer, the wrapper is unboxed to its primitive value. That is why you are getting true always except first condition.

In the first condition, your are checking the reference equality of two different objects, which will be false obviously.

Renjith
  • 3,274
  • 19
  • 39
1

== for classes compares references. When you make 2 objects with the same values you should use .equals() because 2 object the are instantiated with new will have different references. When using int it compares the values because they are primitives and passing in new Integer() into an int is creating a primitive int.

cbender
  • 2,231
  • 1
  • 14
  • 18
1
Integer a = new Integer(10);
Integer b = new Integer(10);

a==b ---> Reference variable a is compared to the reference variable b. Not the object they point to. And those two reference variables are indeed, different. Hence, false. Use .equals() to compare objects.

a==x ---> auto-unboxing. x contains the value 10. during comparison, Integer is compared to int. When wrapper is compared to premitive, auto-unboxing occurs. a becomes an int. And so, effectively 10 is compared to 10. Hence true.

x==p,b==q,y==q,x==y --> Same. Auto-unboxing, taking effect. Hence all true.

Whenever Java compares a wrapper class variable with a primitive variable, it unboxes the wrapper class variable into a primitive, and then compares them.

Compile this with SDK previous to Java 5, and I doubt if it would compile at all. Java introduced this feature from Java 5. If I can remember correctly..

Samrat Dutta
  • 1,727
  • 1
  • 11
  • 23