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?

Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • 2
    Why do you think that "s1, s2 will point to the same object on the heap"? –  Aug 08 '12 at 12:46
  • 3
    Possible duplicate with an answer: http://stackoverflow.com/questions/3637936/java-integer-equals-vs http://stackoverflow.com/a/3637974/227755 `The JVM is caching Integer values. == only works for numbers between -128 and 127` – nurettin Aug 08 '12 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; – Amarnath Aug 08 '12 at 12:53
  • @pwned Yeah thank you very much. I know that. But still can you tell me why is that so? – Amarnath Aug 08 '12 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. – nurettin Aug 08 '12 at 13:00
  • @pwned Ok I will ask why JVM does like that in another post. – Amarnath Aug 08 '12 at 13:02

6 Answers6

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.

Community
  • 1
  • 1
Davz
  • 1,530
  • 11
  • 24
  • Try `-XX:+AggressiveOpts` and similar options and `500` might work. `-129` will never work. – Peter Lawrey Aug 08 '12 at 12:59
  • 1
    depends on how you define 'work' :) my suggestion is just not to do this at all – Joeri Hendrickx Aug 08 '12 at 13:01
  • 1
    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. – Matt Aug 08 '12 at 15:22
0

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

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

Hope this helps.

JetPro
  • 1,044
  • 3
  • 23
  • 43
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.

Community
  • 1
  • 1
Dredj
  • 137
  • 1
  • 2
  • 10
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.

2v0mjdrl
  • 582
  • 4
  • 19
  • Yes agreed. But what is the case with Integer? – Amarnath Aug 08 '12 at 12:53
  • 1
    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". – 2v0mjdrl Aug 08 '12 at 14:07
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

Alexey Ogarkov
  • 2,916
  • 23
  • 28
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

vijay
  • 926
  • 7
  • 15