2

As we know that == in case of Objects returns true if pointing to the same reference else it returns false.

So , if i have taken

    Integer a = new Integer("1"); // Creating Integer Object a
    Integer b = new Integer("1"); // Creating Integer Object b

and then perform a == b , then it returns true but they both have different references.

Vaibhav Jain
  • 187
  • 2
  • 9
  • 2
    Possible duplicate of [Why does 128==128 return false but 127==127 return true when converting to Integer wrappers?](http://stackoverflow.com/questions/1700081/why-does-128-128-return-false-but-127-127-return-true-when-converting-to-integ) – Florent Bayle Jan 10 '17 at 09:29
  • 1
    Are you sure it returns `true`? – Codebender Jan 10 '17 at 09:31

5 Answers5

4

The JVM caches integer values between -127 to 127.
That's the reason == works for Integer value between this range.

But:

Integer i1=new Integer("11");
Integer i2=new Integer("11");
System.out.println(i1==i2); //false

Integer i3=11;
Integer i4=11;
System.out.println(i3==i4); //true

Integer i5=128;
Integer i6=128;
System.out.println(i5==i6); //false
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
  • When using `new` keyword, a new object has to be created and returned. Caches don't work in that case. – Codebender Jan 10 '17 at 09:32
  • does this mean JVM cache works as String constant pool ? – Vaibhav Jain Jan 10 '17 at 09:37
  • Not exactly String pool, because no new integers get added to this cache, Integer class uses Flyweight pattern i.e. Integer.valueOf(..), it will return integer value from this cache. – Amit Bhati Jan 10 '17 at 09:38
2

Because it is overridden. source:

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}
Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
  • but we are not talking about equals method! – Mr.Q Jan 10 '17 at 09:39
  • You're right. Here is an explanation about the same - http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.21 - where it says - **The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error.** – Pavan Kumar Jan 10 '17 at 10:05
1

Let me more elaborate on this, @asAmit Bhati said in his answer when we write

    Integer i1=new Integer("11");
    Integer i2=new Integer("11");
    System.out.println(i1==i2); //false

jvm creates two separate objects and comparing them with "==" results in false. but when we write the following code:

    Integer i3=11;
    Integer i4=11;
    System.out.println(i3==i4); //true

it will be translated into this:

    Integer i3=Integer.valueOf(11);
    Integer i4=Integer.valueOf(11);

Implementation of valueOf method is as follow(in java 1.8):

    public static Integer valueOf(int var0) {
    return var0 >= -128 && var0 <= Integer.IntegerCache.high?Integer.IntegerCache.cache[var0 + 128]:new Integer(var0);
}

as you can see if the value is between -128 and Maximum cache value (which can be configured using this jvm parameter -Djava.lang.Integer.IntegerCache.high), it will retrieve the cached value and doesn't create a new instance of Integer that is why (==) returns true for certain values!

also note that the same goes for Character Wrapper class but not for Float and Double classes.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
Mr.Q
  • 4,316
  • 3
  • 43
  • 40
0

As the answer above says, it may work in many cases, nevertheless you shouldn't compare two Integer with == since it may present problems in some cases.

Check this answer for more information:

Comparing Integer values in Java, strange behavior

Community
  • 1
  • 1
SCouto
  • 7,808
  • 5
  • 32
  • 49
0

If you check equals method implementation in Integer class it is:

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}

From here you can see it uses "==" operator.

Now the reason behind it. Ultimately you have to compare value of the Integer wrapper class, which will happen automatically because of autoboxing and unboxing in Java.

And also from the method definition you can see that it is retrieving passed Object value using ((Integer)obj).intValue().

SachinSarawgi
  • 2,632
  • 20
  • 28
  • i am not asking about equals method , i am asking for == operator , does it means when we call == it internally call equals method.. – Vaibhav Jain Jan 10 '17 at 09:35