大家 先看这个文章 :
http://hi.baidu.com/gabe2008/blog/item/0a96542c16f5973f359bf7d8.html
public class Myclass {
   public static Integer starting;
   public static void methodA(){
      Integer i = new Integer(25);
      starting = i;
      methodB(i);
   }
   
   private static void methodB(Integer i2){
      i2 = i2.intValue();
      if(i2==starting) System.out.println("1 true");  //按理说,i2==starting 在128之内能自动解开的。
      if(i2.equals(starting))System.out.println("2 true");
      else {
         System.out.println("false");
      }
   }
   public static void main(String[] args) {
      methodA();
   }
}

解决方案 »

  1.   

     i2 = i2.intValue();可i2已经定义成 Integer了,故这样搞一下,它并不就变成 int了, 所以(i2==starting) 是false的.
      

  2.   

    i2 是INTEGER 类型。所以说
      

  3.   

    i2 = i2.intValue();这行代码注掉
    输出:1 true
         2 true
      

  4.   

    i2 = i2.intValue();此时i2已经是int了。我也是这么认为
      

  5.   

    starting是Integer的,Integer是不可变的。
      

  6.   

    不过学了这段话
    在Integer在自动装箱时对于值从-128~127之间的值,它们被装箱为Integer对象后,会在内存中被重用,实际上是参考至同一个对象(即a与b指向同一个对象)。如果超过了-128~127之间的值,被装箱后的Integer对象并不会被重用,即相当于每次装箱时都新建了一个Integer对象,
      

  7.   

    因为starting相当于Integer i = new Integer(25);中的i。
    是一个new出来的Integer对象。
    如果Integer i=25。那么应该能输出
    1 true
    2 true 
      

  8.   

    Interger是类,a==b并不是简单的25==25。
    a==b的条件是两个类的引用相等,而不是他们的intValue()相等,
    因此,想达到目的,可采用intValue方法比较他们的值相等。初学者,不知理解对不对
      

  9.   

    因为Integer是一个final类,即不可变
    所以i2 = i2.intValue();这一句时,自动包装机制将25打包成一个新的Integer对象返回,所以i2==starting的值是false
      

  10.   

    包装机制的源代码:    public static Integer valueOf(int i) {
            if(i >= -128 && i <= IntegerCache.high)
                return IntegerCache.cache[i + 128];
            else
                return new Integer(i);
        }
      

  11.   

        private static class IntegerCache {
            static final int high;
            static final Integer cache[];        static {
                final int low = -128;            // high value may be configured by property
                int h = 127;
                if (integerCacheHighPropValue != null) {
                    // Use Long.decode here to avoid invoking methods that
                    // require Integer's autoboxing cache to be initialized
                    int i = Long.decode(integerCacheHighPropValue).intValue();
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - -low);
                }
                high = h;            cache = new Integer[(high - low) + 1];
                int j = low;
                for(int k = 0; k < cache.length; k++)
                    cache[k] = new Integer(j++);
            }        private IntegerCache() {}
        }