3

Possible Duplicate:
Wrapper class and == operator

Saw this code in a website when i was learning about autoboxing..

Integer i1 = 1;  
Integer i2 = 1;  
// true 
System.out.println(i1 == i2);  


Integer i3 = -200;  
Integer i4 = -200;  
// false
System.out.println(i3 == i4);   

I can understand why the 2nd comparison gives false (its comparing references). But why is it giving true for the first one ?

Community
  • 1
  • 1
raj
  • 3,769
  • 4
  • 25
  • 43
  • Thanks a lot for pointing out.. answers there were more explanatory! :) – raj Jul 27 '11 at 16:27
  • 1
    answered already: http://stackoverflow.com/questions/5117132/wrapper-objects-share-the-same-address-space-only-within-the-value-127/5117175#5117175 – Grzegorz Szpetkowski Jul 27 '11 at 16:27
  • Perhaps you could have asked the person who wrote it what it meant. e.g. ask the question in a comment. ;) http://vanillajava.blogspot.com/2011/07/incorrect-core-java-interview-answers.html#comments – Peter Lawrey Jul 27 '11 at 16:57
  • You see, i was seeing this : http://java.dzone.com/articles/incorrect-core-java-interview for which i had to "register" for posting comments.. – raj Jul 27 '11 at 17:05

2 Answers2

7

Because the first several Integer objects (from -128 to 127, inclusive, to be precise) are cached and reused by the JVM, so i1 and i2 are references to the same physical object.

This is also true to Long, Short and Byte btw. See this article for a more detailed explanation.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Could you explain this further? If the first several Integer objects are cached, why aren't i3 and i4 pointing to the same physical object? – Kyle Jul 27 '11 at 16:27
  • @Kyle, extended my answer with more details and an article reference. – Péter Török Jul 27 '11 at 16:29
  • are other objects of classes like Strings cached (cos i just tried "a" == "a" .. and it gave me true)? can u point me to a article or something related to caching? – raj Jul 27 '11 at 16:50
  • @raj: That's a case of *string literals* being interned. Again, see the specification - I can find a section number if you want. – Jon Skeet Jul 27 '11 at 16:54
  • Thanks.. I just finished downloading that.. – raj Jul 27 '11 at 17:11
3

Boxing is guaranteed to use the same cached objects for a range of values.

Beyond that the JVM can use a larger cache, but it's not guaranteed. From the JLS section 5.1.7:

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The Integer documentation [says](http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#valueOf%28int%29) that frequently requested values may be cached. – Rag Jul 27 '11 at 16:29
  • 1
    @Brian: Indeed. It's just not guaranteed *apart from* the values in the spec. – Jon Skeet Jul 27 '11 at 16:43
  • 1
    +1: `Long` also has a cache for values -128 to 127 in the Sun/Oracle JVM even though it is not mentioned. – Peter Lawrey Jul 27 '11 at 17:04