http://blog.csdn.net/KingWolfOfSky/archive/2009/08/13/4444231.aspx
这篇博客里面讲了些关于Java细节方面的内容,它给了下面段代码,对输出还是不理解,麻烦各位大虾给解释下~~   1.   public static void main(String args[])  
   2.   {  
   3.       Integer i1=new Integer(13);  
   4.       Integer i2=new Integer(13);  
   5.       int i3=13;  
   6.       System.out.println(i1==i2);  
   7.       System.out.println(i2==i3);  
   8.       System.out.println(i3==i1);  
   9.   }  
  10. * Output  
  11. * true  
  12. * false  
  13. * true  
  14. */  
还有他说了自动包装时对于从–128到127之间的值,它们被包装为Integer对象后,会存在内存中被重用,而其它的值,被包装后的Integer对象并不会被重用,即相当于每次包装时都新建一个Integer对象。
这是为什么?
小弟新人,先谢谢各位了~~

解决方案 »

  1.   

    public static Integer valueOf(int i) {
          final int offset = 128;
          if (i >= -128 && i <= 127) { // must cache 
             return IntegerCache.cache[i + offset];
          }
           return new Integer(i);
        }
    当你直接给一个Integer对象一个int值的时候,其实他是调用了valueOf这个方法
    //IntegerCache 是Integer的一个内部类,作用是在第一次调用
    //时对-128到-127之间的整数进行装箱,cashe数组时final修饰的
    //,因此它是不能改变的变量,其值与第一次调用时的值是一样的
    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 = new Integer(i - 128);
    }
        }
    上面两个方法都是Integer里面方法的源码
      

  2.   

    我运行的结果是
    false
    true
    true
      

  3.   

    老帖子
    地址:
    http://topic.csdn.net/u/20090412/15/52006dd1-824b-459b-8b58-d22063ca8fc4.html
      

  4.   

    值在–128到127之间的Integer在常量池中放着,你新new的Integer的值只要在–128到127之间之间,实际上是不新建对象的,而是直接从常量池中取,所以无论你new Integer(20)多少次,他们的引用都是相同的。i1==i2 //两个引用相同。
    i2==i3 //这个会把i3自动包装为Integer,由于i3是变量,所以会在堆中建新的对象。  
    i3==i1 //这个会把i1自动解包为int。上面两式说明,自动包装和解包是以运算符左边的操作数的类型为主的。
      

  5.   

    看来用new 是会在堆中建新对象的.用Integer i1=13;时用调用Integer valueOf(13)这时用的是常量池中的对象.
      

  6.   

    莫非JDK1.6发生了某种变化..public class Test {
    public static void main(String[] args) {
    Integer[] in=new Integer[10];
    for(int i=0;i<10;i++){
     in[i]=new Integer(20);
    }
    for(int i=0;i<9;i++){
    System.out.println(in[i]==in[i+1]);
    }
    }}
    ================
    结果:
    false
    false
    false
    false
    false
    false
    false
    false
    false
      

  7.   

    我终于明白了 是那博客作者笔误了 他已经指出“对于Integer i = new Integer (int);的代码,则一概在堆中创建新对象,而不管其字符串值是否相等,是否有必要创建新对象,从而加重了程序的负担。” 却在程序输出里写错了
      

  8.   

    楼主那段代码,输出应该是
    false//new出来的对象都是新的对象,==比较两个对象是比较地址值,两个不同对象的地址是不可能相同的
    true//应该是比较值吧
    true
      

  9.   

    我也是
    楼主那段代码
    System.out.println(i2==i3);  
    System.out.println(i3==i1); 
    用int类型的i3和Integer的变量来比较,这时候的==应该是直接比较两者的int值
    public static void main(String args[]) {
    Integer i1 = new Integer(1123);
    int i = 1123;
    System.out.println(i == i1); //true
    }
      

  10.   


    public static void main(String args[]) {
    Integer i1 = new Integer(1123);
    int i = 1123;
    Integer i2 = 1123;
    System.out.println(i == i1); //true
    System.out.println(i == i2); //true
    System.out.println(i1 == i2); //false
    }
      

  11.   

    听我说,其实是这样的:
    首先:运行结果不是true false true,而是false true true。
    原因:由于包装类的对象是不可变的,所以两个拥有相同类型相同值的数可以使用同一个包装类对象,因此在这种情况下,在Java实现中这两个数自动装箱所使用的包装类对象是同一个包装类对象。但是这两个数必须满足以下条件:
    数的类型          数的值域
    boolean         true与false
    byte         所有值
    char         \u0000~\u00ff
    short         -128~127
    int         -128~127就这些,SUN的官方资料。
      

  12.   

    给你举个例子吧:public class MyJava{
    public static void main(String[] args){
    make(3,3); //调用后打印结果为true
    make(500,500); //调用后打印结果为false
    make(3,new Integer(3)); //调用后打印结果为false
    make(new Integer(3),new Integer(3)); //调用后打印结果为false
    }
    public static void make(Integer a,Integer b){
    System.out.println(a==b);
    }
    }
      

  13.   

    Integer i1=new Integer(13);  使用new就是重新创建了对象,而不是在池中拿
    Integer i1=3;和Integer i1=Integer.valueOf(3);这两条语句是一样的,这是自动封箱
    Int a=i1;和int a=i1.intValue()一样;这是自动解封。
      

  14.   


    你是不是搞错了,所以无论你new Integer(20)多少次,他们的引用都是相同的,这句话对吗?new 永远是新创建的,改成Integer i=20;或者是Integer i=Integer.valueOf(20);这样才是你说的那个直接从常量池中取
      

  15.   

    jdk 1.4 之前没有对-128-127做缓存,之后都做为缓存。。
    呵呵
      

  16.   

    false 
    true 
    true
    ==================
    Integer i1=new Integer(13);是new了个integer,在堆中生成
    Integer i2=new Integer(13);也是new了个integer,同上,因此i1!=i2;
    int i3=13; 是从池中取的,自动封装
      

  17.   


    public static void main(String args[]) {
            Integer i1 = new Integer(123);
            int i = 123;
            Integer i2 = 123;
            System.out.println(i == i1); //true
            System.out.println(i == i2); //true
            System.out.println(i1 == i2); //false
        }由这个结果可以看出来int和Integer比较的话是比较两者的int值
    而两个Integer对象互相比较的是两者的引用是否一样
      

  18.   

    我记得我的运行结果页跟楼主不一样,false,true,true
      

  19.   

     public static void main(String args[])  
       2.   {  
       3.       Integer i1=new Integer(13);  
       4.       Integer i2=new Integer(13);  
       5.       int i3=13;  
       6.       System.out.println(i1.intValue()==i2.intValue());  
       7.       System.out.println(i2==i3);  
       8.       System.out.println(i3==i1);  
       9.   }  
      10. * Output  
      11. * true  
      12. * true
      13. * true