public class IntegerUnder128 {
   public static void main(String[] args) {
      Integer a = 10;
      Integer b = 10;
      if(a==b){  //这里比较是用自动拆箱,那么如何证明a与b是堆里的两个不同对象
         System.out.println("a == b"); 
         System.out.println(a);
         System.out.println(b);
      }else{
         System.out.println("他们不相等!");
      }
   }
}我觉得他们是建立了两个对象,但比较时是被自动拆箱的。

解决方案 »

  1.   

    不是建立2个对象吧
    估计是和那个 string pool 差不多
      

  2.   

    see valueOf方法,如果没记错的话100以内是池化的,100以上new的
      

  3.   

    -128 to 127
    是池化的。
    我明显明白,
    问题是:
    a 与 b是两个不同的对象。
    如果证明。可以输出他们的内存地址吗?
      

  4.   

    楼上说的点是对的,但是“池化”也就是预先创建的有 -128~0,0~127 就是128+127+1个对象,不是100以内。
    整数类型(Long,Integer,Short,Byte)都有相同处理(“池化”)。Character也有“池化”,不过不是-128~127,是0到127.
      

  5.   

    从你的class name 上可以看出你明显明白,<code>Integer a = 10;
          Integer b = 10;
    </code>小于127 很明显 a,b引用指向的都是catch里面的同一个对象new Integer(10)
      

  6.   

    附上Integer中“池”的源代码:    private static class IntegerCache {
    private IntegerCache(){} static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static {
        for(int i = 0; i < cache.length; i++)
    cache[i] = new Integer(i - 128);
    }
        }
      

  7.   

     Integer的hashCode就是他的int value; :)
      

  8.   

    public static Integer valueOf(int i) {
            if(i >= -128 && i <= IntegerCache.high)
                return IntegerCache.cache[i + 128];
            else
                return new Integer(i);
        }
      

  9.   

    a和b应该是同一个对象 ==比较的应该是对象地址,没有拆箱
    可以试试                      Integer a = new Integer(10);
               Integer b = new Integer(10);
               if(a==b){
               //if(a.equals(b)){
                  System.out.println("a == b"); 
                  System.out.println(a);
                  System.out.println(b);
               }else{
                  System.out.println("a != b");
               }
      

  10.   

    确实只是在128以下是这样
    以上就是两个对象
    不过if(a==b)应该不拆箱
      

  11.   

    ==和!=是不会拆箱的,只有运算符会Integer a = 200;
    Integer b = 200;a>=b :true
    a<=b :true
    a!=b :true
      

  12.   

    这个属于JVM的实现问题,哪天计算机都NB了,+-1024之内的数字都被"池"了呢?
      

  13.   

    看下Integer类的源码就知道了 是同一个对象
      

  14.   

    让我终结了吧:
    1. Integer a = 10;  Java在编译的时候是执行的这个操作:Integer a = Integer.valueOf(10);
    2. Integer.valueOf(10) 10在-128到127之间,所以 Integer a = IntegerCache.cache[i + offset];Integer b = 10; 同上 即 Integer b = IntegerCache.cache[i + offset];结果很明显了。
      

  15.   

    oh~~直接贴了源代码没注意,解释下Integer a = IntegerCache.cache[i + offset]; Integer中offset是128,这里的i就是10;关于IntegerCache 的源代码我在8楼已经贴出来了,或者直接查看源码。
      

  16.   

    Integer a = 10;
     Integer b = 10;
    能用吗????? 放到IDE中试试。
    1.
    Integer a = new Integer(10);
     Integer b = new Integer(10);
    这时是新实例化了两个引用类型对象,他们是两个对象(两个NEW).
    2.
    int a=10;
    int b=10;
    int型的就是字面量,不是引用类型,一直就是一个对象呀。
    3.
    Integer a = Integer.valueOf(10);
    Integer b = Integer.valueOf(10);
    一个对象,都指向堆中的10。
      

  17.   

    现在已经可配置了。http://rednaxelafx.javaeye.com/blog/680746
      

  18.   

    学习!怎么会是true...
    没想到小于128会是true
      

  19.   

    new Integer(),-128到127用的都是缓存里的引用。一个
      

  20.   

    过来凑个热闹。
    用Eclipse的debug功能,我得到了如下的结论。
    Integer a = 10;这里发生了auto-boxing,没什么疑问;
    但是怎么装箱的呢?
    step into得到以下代码:
        public static Integer valueOf(int i) {
            if(i >= -128 && i <= IntegerCache.high)
                return IntegerCache.cache[i + 128];
            else
                return new Integer(i);
        }
    基本上猜也可以得到结论(我没有仔细去看high的定义),a和b都是引用同一个对象了;
    能不能确定呢?让我们来看debug给出的显示
    a Integer (id=18)
    b Integer (id=18)
    在我几次的debug过程中,id的值是有变化的,但是每次a和b的id都是相同的,我认为id就是对象的id,故a和b引用同一对象;
    另外说说a == b的时候有没有auto-unboxing呢?
    可以加上int c = a;
    step into可以看到解箱代码:
        public int intValue() {
    return value;
        }
    而在执行a == b的计算的时候step into发现并未执行上述代码,可以确认,这里的==没有解箱,只是比较a和b是否引用同一对象,由上,得到true的结果。