2
main(){

Integer i1 = 500;

Integer i2 = 500;

System.out.println(i1 == i2);  // O/P is "**false**"

String s1 = "Hello";

String s2 = "Hello";

System.out.println(s1 == s2);  // O/P is "**true**"

} // End of main.

I am not able to figure out why the output is different. As far as I know s1, s2 will point to the same object on the heap. So their reference address are same. Similarly I think Integer is also the same. But it is not. Why is it different?

6
  • 2
    Why do you think that "s1, s2 will point to the same object on the heap"? Commented Aug 8, 2012 at 12:46
  • 3
    Possible duplicate with an answer: stackoverflow.com/questions/3637936/java-integer-equals-vs stackoverflow.com/a/3637974/227755 The JVM is caching Integer values. == only works for numbers between -128 and 127 Commented Aug 8, 2012 at 12:48
  • @Tichodroma B'coz s1, s2 are declared with out using new keyword. So same reference will be given to both s1, s1; Commented Aug 8, 2012 at 12:53
  • @pwned Yeah thank you very much. I know that. But still can you tell me why is that so? Commented Aug 8, 2012 at 12:55
  • @Amarnath Are you asking why JVM only caches byte sized integers? I didn't see any indication of that in your question. If that is what you wanted to ask in the first place, I suggest you modify the question before you get more answers. Commented Aug 8, 2012 at 13:00

6 Answers 6

8

It has been already answered here: java: Integer equals vs. ==

Taken from this post: The JVM is caching Integer values. == only works for numbers between -128 and 127. So it doesn't work with 500 in your example.

Sign up to request clarification or add additional context in comments.

3 Comments

Try -XX:+AggressiveOpts and similar options and 500 might work. -129 will never work.
depends on how you define 'work' :) my suggestion is just not to do this at all
And as a side note, it's working for string here, because you're using a string literal in your code, which ends up getting interned, which means that both variables will point to the same string instance.
0

Use string1.equals(string2); //Used for String values

Instead of using string1 == string2; //Used for integer values

Hope this helps.

Comments

0

The answer in this question should help explain it: How to properly compare two Integers in Java?

Pretty much you answered your own question. The "==" is not only comparing the reference points in strings, but it seems to do the same thing with integers.

Comments

0

The way you specified "Hello" does no heap allocation. Instead, "Hello" as a static compile-time constant will be outsourced to the specific section of the executable and referenced. Thus, both references will point to the same address.

2 Comments

Yes agreed. But what is the case with Integer?
With Integer, the opposite happens (outside the -127 - 128 cache range mentioned above): You create two separate Integer objects i1 and i2 with the same value, but different locations in memory. Thus, == will answer "false", but .Equals() will answer "true".
0

So there is Java String Pool and here s1 and s2 actually the same links. In case of Integers, there is also pool but it exists only for Integers -127 till 128

So if you have

Integer i1 = 100;

Integer i2 = 100;

Then i1==i2 will be true

3 Comments

Yaa I know that it will be true until both of the Integer object values are < 128. But why is that?
The same thing, there is pool in PermGen for integers like that.
You can look at owasp.org/index.php/… for reference
0

"==" always compare the memory location or object references of the values. equals method always compare the values.but equals also indirectly uses the "==" operator to compare the values. Integer uses Integer cache to store the values from -128 to +127.If == operator is used to check for any values between -128 to 127 then it returns true. for other than these values it returns false .

In string, if string is initialized like this

    String s1="abc"
    String s2="abc"

String s1 and s2 are pointing the same location in memory or String pool.

if string is initialized like this

    String s1=new String("abc"); 
    String s2=new String("abc");

String s1 is pointing the new location in which it contains String "abc" String s2 is pointing the new location in which it contains String "abc" but s1's location is different from s2's location.In that situation equals method is useful for string comparison.

Refer the link for some additional info

Comments

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.