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);
    }
}
            Asked
            
        
        
            Active
            
        
            Viewed 58 times
        
    -3
            
            
        - 
                    This has been around since 2004 when Java 5.0 was introduced. You might expect this has been answered before ;) – Peter Lawrey Jul 08 '14 at 07:43
- 
                    @PeterLawrey - answered several times by several different people.. :P – TheLostMind Jul 08 '14 at 12:49
1 Answers
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
- 
                    2Side 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
 
    