5

Possible Duplicate:
Integer wrapper objects share the same instances only within the value 127?
How != and == operators work on Integers in Java?

I tried to compare two ints with the following cases and got unexpected results

  1. when I did the following, @@@ was printed.

     class C {
       static Integer a = 127;
       static Integer b = 127;
       public static void main(String args[]){
       if(a==b){
          System.out.println("@@@"); 
       }
       }
     }
    
  2. when I did the following, @@@ was not printed.

     class C {
       static Integer a = 145;
       static Integer b = 145;
       public static void main(String args[]){
       if(a==b){
          System.out.println("@@@"); 
       }
       }
     }
    

Can anyone tell me what could be the reason.

Community
  • 1
  • 1
Rookie
  • 8,660
  • 17
  • 58
  • 91
  • If someone could format it..I am not able to... – Rookie Aug 30 '12 at 21:09
  • 4
    possible duplicate of [Integer wrapper objects share the same instances only within the value 127?](http://stackoverflow.com/questions/5117132/integer-wrapper-objects-share-the-same-instances-only-within-the-value-127) and [New Integer vs valueOf](http://stackoverflow.com/questions/2974561) and [Inconsistent behavior on java's ==](http://stackoverflow.com/questions/1148805) and [Integer wrapper objects share the same instances only within the value 127?](http://stackoverflow.com/questions/5117132) and... – Tomasz Nurkiewicz Aug 30 '12 at 21:09
  • 2
    More details if you are interested http://vanillajava.blogspot.co.uk/2012/01/surprising-results-of-autoboxing.html – Peter Lawrey Aug 30 '12 at 21:20
  • hey this looks good explanation..will read it – Rookie Aug 30 '12 at 21:23

4 Answers4

7

You're comparing the objects' identities. For values lower than 128 the Integer class caches its objects. That's why it is equal in the first example. The other example is with higher values that are not cached.

As @niklon pointed out there is also a lower border of -128 for caching.

Upper border can be adjusted with a VM arg -Djava.lang.Integer.IntegerCache.high=4711.

Further reading in Peter's interesting blog post: http://vanillajava.blogspot.co.uk/2012/01/surprising-results-of-autoboxing.html

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
5

You're not comparing ints, you're comparing objects for reference equality. Use .equals, or use type int instead of Object.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 1
    /Then why is it printing "@@@" in the first case..? – Rookie Aug 30 '12 at 21:11
  • 2
    @Raghav, because many versions of Java [memoize small `Integer`s](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf%28int%29): "This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range." – Mike Samuel Aug 30 '12 at 21:12
1

Here you are using Integer objects as opposed to int primitives. Hence, you should compare the two instances with .equals(...) as opposed to ==. If you used the primitive type instead, you would use ==.

It is important to note that, when dealing with objects, == compares the references of the two objects, not the actual values - so it may return seemingly strange results at times.

arshajii
  • 127,459
  • 24
  • 238
  • 287
1

Use if(a.equals(b)) and do not use == to compare Objects that are subclasses of Object class.

== operator is just for primitive types like int,long, etc.

Mehdi
  • 4,396
  • 4
  • 29
  • 30