1

I have a question about the following piece of code:

public class Equivalence {
    public static void main(String[] args) {
        Integer n1 = new Integer(47);
        Integer n2 = new Integer(47);
        System.out.println(n1 == n2);
        System.out.println(n1 != n2);
    }
}

The resulting output surprised me:

false
true

I have checked the constructor in Javadoc online, nothing help from there.

Thanks in advance

Thank you

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
Venzentx
  • 487
  • 3
  • 6
  • 17

5 Answers5

4

You should use equals to check value equality. Because Integer is wrapper class which wraps int value. == checks equality based on reference so in both cases your reference is different.

Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.

 n1.equals(n2);

If you do

int n1 = new Integer(47);
int n2 = new Integer(47);

You will get output you are expecting.

Because Java maintains pool of integers from 127 to -128 you can also do

 Integer n1 = Integer.valueOf(47);
 Integer n2 = Integer.valueOf(47);<-- will return you the same reference
     n1==n2 >>> True
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • Thanks, but the two values (n1 and n2) are seem the same, why the result in first output is False please ? when I use System.out.println(n1.equals(n2)), it goes right as I thought. – Venzentx Nov 03 '12 at 20:24
3

Only primitive types can be reliably compared with ==. For objects (and Integer is an object type), the equals() method should be used.

== can be used for objects but only to check whether two objects are in fact the same. So for example:

Integer n1 = new Integer(47);
Integer n2 = n1;
// n1 == n2 will be true

It's best if you think of ints as the number itself and Integers as a post-it note with the number written on it. == will only return true if you're talking about the same post-it note, equals() however will return true for any two notes with the same number on it.

What complicates the issue is that since version 1.5 Java also has autoboxing, that is, ints are automatically converted to Integer and vice versa when required. This can lead to very surprising results if you're not careful.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • the answer is very clear, thank you ! – Venzentx Nov 03 '12 at 20:31
  • I have another question here, what happened to String if I want to create 2 different objects and give them 2 same contents, but the result is false by using s1.equals(s2). Thanks – Venzentx Nov 04 '12 at 13:44
1

n1 == n2, compares the object instances and hence n1 == n2 will return false and n1 != n2 will return true as n1 and n2 are not same object instances.

If you use equals method or get int (primitive tpye) value and then compare using == or != you will get right results e.g.

    System.out.println(n1.equals(n2)); //true
    System.out.println(!n1.equals(n2));//false

or

    System.out.println(n1.intValue() == n2.intValue()); //true
    System.out.println(n1.intValue() != n2.intValue()); //false
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

I believe it's because you're not comparing the value of n1 and n2 you're comparing the object reference. And since they are two different objects, their references are NOT the same.

Thus, you should use the equals() method to compare their values

Force444
  • 3,321
  • 9
  • 39
  • 77
1

java.Lang.Integer is an object in java. you should check if two Integer objects are equals using equals() method

you are checking if n1 reference to Integer object and n2 reference to another Integer object are referring to the same Integer Object. because == operator checks if two reference variables refer to the same object. in this case they aren't. try

              Integer n1 = New Integer(47);
              Integer n2 = New Integer(47);
              System.out.println(n1.equals(n2));
              System.out.println(n1.equals(n2));
PermGenError
  • 45,977
  • 8
  • 87
  • 106