请看下面两段代码:
public class AutoBoxDemo2 {
    public static void main(String[] args) {
        Integer i1 = 100;
        Integer i2 = 100;        if (i1 == i2) 
            System.out.println("i1 == i2");
        else 
            System.out.println("i1 != i2");
    }
}
——————我是分割线——————public class AutoBoxDemo3 {
    public static void main(String[] args) {
        Integer i1 = 200;
        Integer i2 = 200;
 
        if (i1 == i2) 
            System.out.println("i1 == i2");
        else 
            System.out.println("i1 != i2");
    }
}为什么第一段代码执行出来是i1 = i2接着执行第二个却是 i1 != i2 ??? 

解决方案 »

  1.   

    好像是采取缓存的机制
    对-128到-127之间的整数进行装箱时会先看看缓存中是否存在,若已存在则指向已经有的这个对象楼主可以参考
    http://topic.csdn.net/u/20090412/15/52006dd1-824b-459b-8b58-d22063ca8fc4.html
      

  2.   

    Integer取值范围 -128到127
    看下面代码:public void testCsdnJava();{   
            Integer b1 = -128;   
            Integer b2 = -128;   
            System.out.println( b1==b2);;   
            Integer c1 = -129;   
            Integer c2 = -129;   
            System.out.println( c1==c2);;          
               
            Integer j1 = 127;   
            Integer j2 = 127;   
            System.out.println( j1==j2);;   
            Integer k1 = 128;   
            Integer k2 = 128;   
            System.out.println( k1==k2);;   
               
               
        }     
      
      
    result:   
      
      
    true  
    false  
    ture   
    false  
      

  3.   

    Character 0到127
    Long -128到127
    Integer -128到127
    Byte 的所有范围
    Short -128到127这些都是在第一次装箱时放在缓存里面的,以后装箱时都指向第一次装箱产生的对象