Object obj = 45; //这里被自动的autoboxing 

Object anotherobj = 45;

if(obj == anotherobj )  //这里我跟踪了下他并没有调用intvalue这个方法,所以没有发送unboxing
System.out.println("怎么会相等呢?????");
if(obj == anotherobj ) 这句并没有unboxing,怎么居然还相等了呢?实在 是不解啊。望指教!!

解决方案 »

  1.   

    去看Integer类的valueOf方法实现
      

  2.   

    -128到127的数字在装箱时会得到缓存中的Integer.IntegerCache.cache中的Integer对象,所以obj==anotherObj
      

  3.   

    Thinking Java --> effective Java --> Inside the Java Virtual Machine
      

  4.   

    才看了下英文effective java 中的item 50 讲得确实不错,跟effective c++ 有得一拼啊,决定明天买effective java了
      

  5.   

    同意这个!
    Integer.valueOf()静态方法总是返回一个Integer实例。
      

  6.   

    哦,原来如此,谢谢1/2楼 的朋友 
    Object obj = 45;  是被翻译成 Object isobj = Integer.valueOf(32);
    是这样的吗?且 i 必须在 -128 <= i <= IntegerCache.high 才有这个范围的缓存对象,否者建立一个的新的对象。
      

  7.   

      Code:
       0:   bipush  16
       2:   invokestatic    #16; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
    teger;
       5:   astore_1
       6:   return
    已证实我的猜想。谢谢各位的回答!结帖