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.

2

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

3 Comments

When using new keyword, a new object has to be created and returned. Caches don't work in that case.
does this mean JVM cache works as String constant pool ?
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.
2

Because it is overridden. source:

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

2 Comments

but we are not talking about equals method!
You're right. Here is an explanation about the same - 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.
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.

Comments

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

Comments

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

1 Comment

i am not asking about equals method , i am asking for == operator , does it means when we call == it internally call equals method..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.