0

I have a question. I don't know this makes any sense but i really need an answer

when i use "==" as

Integer i1 =10;
Integer i2 = 10;
if(i1 == i2) {System.out.println("same object")}

output = same object

However , if i use "=="

String obj1 = new String("xyz");

String obj2 = new String("xyz");

if(obj1 == obj2)
   System.out.println("obj1==obj2 is TRUE");
else
  System.out.println("obj1==obj2 is FALSE");

output = FALSE

I know that "==" operator looks for the memory location of the object. But what happened in first example; i1 and i2 are not a different objects?

user2985842
  • 437
  • 9
  • 24
  • Integer is is automatically cast to an int – Brad Jan 15 '14 at 17:21
  • take a look at http://stackoverflow.com/questions/1514910/when-comparing-two-integers-in-java-does-auto-unboxing-occur – Алексей Jan 15 '14 at 17:22
  • This has nothing to do with automatic casts or unboxing. Integers in the range [-128, 127] are cached in a pool, that's all. – Jeroen Vannevel Jan 15 '14 at 17:23
  • Yes but for `Integer i1 =10; Integer i2 = new Integer(10);`. `==` will return false. So be careful with the cache. Don't rely on it and always use equals. – user2336315 Jan 15 '14 at 17:25
  • http://stackoverflow.com/questions/20897020/why-integer-class-caching-values-in-the-range-128-to-127 + http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?lq=1 => your answer – assylias Jan 15 '14 at 17:26
  • This is not a duplicate! Those other questions do not address the caching issue. – Lyn Headley Jan 15 '14 at 17:32

2 Answers2

6

There's internal cache/pool for small integers. So the == check returns true as they point to the same actual object.

OP, btw in your question you have typed "if(i1==12)" instead of "if(i1==i2)".

Example 1:

public class Test001
{

    public static void main(String args[]) {
        Integer i1 = 10;
        Integer i2 = 10;
        if(i1==i2) {System.out.println("same object 1");}

        i1 = 10024;
        i2 = 10024;
        if(i1==i2) {System.out.println("same object 2");}
    }
}

It prints only "same object 1" which demonstrates the cache/pool point.

The fact that you assign i1 and i2 to the literal 10 is important here. If you did new Integer(10) then they would point to different objects.

Example 2:

public class Test001
{

    public static void main(String args[]) {
        Integer i1 = 10;
        Integer i2 = 10;
        if(i1==i2) {System.out.println("same object 1");}

        i1 = new Integer(10);
        i2 = new Integer(10);
        if(i1==i2) {System.out.println("same object 2");}

        i1 = 10024;
        i2 = 10024;
        if(i1==i2) {System.out.println("same object 3");}
    }
}

This one also prints only "same object 1".

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • I like you assumed this was an object reference/cache question but apparently others disagree :/ – Jason Sperske Jan 15 '14 at 17:24
  • @JasonSperske Well, it is. – peter.petrov Jan 15 '14 at 17:25
  • @JasonSperske Those who disagree are wrong then... – assylias Jan 15 '14 at 17:26
  • @JasonSperske: this is the answer to his first question about `Integer` being equal when using `==`, which is because of the integer pool. The answer isn't complete for the entire question, but the rest is answered with the answers in related questions. – Jeroen Vannevel Jan 15 '14 at 17:26
  • This is similar to how Strings behave. If you change your code to assign the string variables to "xyz" instead of new String("xyz") you should see == true for the Strings as well. The fact that you explicitly say new is forcing a new Object to be created. If you used new Integer(10) I assume they would not be == either. – moliveira Jan 15 '14 at 17:29
  • Can't believe this was marked as a duplicate. – Lyn Headley Jan 15 '14 at 17:32
  • `Integer i1 = new Integer(10); Integer i2 = new Integer(10); System.out.println(i1 == i2); ` that is false – Алексей Jan 15 '14 at 17:33
  • @user1631616 Yes, I guess (roughly speaking) the OP can think of "Integer i1 = 10; Integer i2 = 10;" as equivalent to "Integer c=10; Integer i1 = c; Integer i2 = c;" where c is that constant from the pool. Hope this is a good analogy. Not sure though if it is. So I did not include it in the answer text. – peter.petrov Jan 15 '14 at 17:37
  • The way it always made since to me is to remember that values are immutable. If I have a variable set to equal 10 and I give it a new value I am not redefining the value 10, I'm redefining my variable. There are some great talks by the creator of Clojure that should be required viewing by any Java programmer (check out Clojure for Java Programmers) – Jason Sperske Jan 15 '14 at 17:50
1

.equals() compares the actual "content" of the String itself, whilst == checks to see if the object references are pointing to the same instance of an object.

adaam
  • 3,700
  • 7
  • 27
  • 51