-3
public class Test
{
    public static void main(String ar[])
    {
        Integer a = 10;
        Integer b =10;
        Integer c = 145;
        Integer d = 145;
        System.out.println(a==b);
        System.out.println(c==d);
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Prasad
  • 15

1 Answers1

5

Integer class keeps a local cache for values between -128 and 127.. and returns the same object.

    Integer a = 10;
    Integer b =10;
    Integer c = 145;
    Integer d = 145;
    System.out.println(a==b); // so, a and b are references to the same object --> prints true
    System.out.println(c==d);// so, c and d are references to different objects --> returns false
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 2
    Side note: It's possible to alter the size of this cache using the `-XX:AutoBoxCacheMax=` argument on the HotSpot VM, so it's entirely possible that `c == d` could return `true`. – awksp Jul 08 '14 at 06:39