Integer a = 127;
Integer b = 127;
a==b 为true;
为什么当a和b的值大于127的时候
a==b 为false了

解决方案 »

  1.   

    这个看JDK源代码,它里有一个数组,它们的值是-128->127,当使用自动装箱时,如果 值在这个范围内,就会返回同一个Integer,所以==为true.
      

  2.   

    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);
    }
        }
    /**
         * Returns a <tt>Integer</tt> instance representing the specified
         * <tt>int</tt> value.
         * If a new <tt>Integer</tt> instance is not required, this method
         * should generally be used in preference to the constructor
         * {@link #Integer(int)}, as this method is likely to yield
         * significantly better space and time performance by caching
         * frequently requested values.
         *
         * @param  i an <code>int</code> value.
         * @return a <tt>Integer</tt> instance representing <tt>i</tt>.
         * @since  1.5
         */
        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);
        }
      

  3.   

    看源码就知道了,127取得的缓存数据  
    static final Integer cache[] = new Integer[-(-128) + 127 + 1];
    static {
        for(int i = 0; i < cache.length; i++)
    cache[i] = new Integer(i - 128);
    }
        }
    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);
        }
      

  4.   

    下面是源码: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);
    }
        }public static Integer valueOf(int i) {
        final int offset = 128;
        if (i >= -128 && i <= 127) {
            return IntegerCache.cache[i + offset];
        }
            return new Integer(i);
        }首先说明的一点是Integer是引用类型,什么是引用类型呢?
    而引用类型就是那些可以通过new来创建对象的类型(基本上都是派生自Object)。
    所有的奥秘都在上面了。下面我帮你解释一下这段代码吧。不过我不敢保证解释得完全对,尽力而为吧.
    java现在是通过jvm来自动装箱的。但jvm实际上是自动调用的valueOf()这个方法。很明显,Integer e= 10其实就是Integer.valueOf(10),也就是说在-128到127都被静态化了,静态化的变量地址是不变的。所以Integer e = 10与Integer f = 10;System.out.println(e==f);这个肯定为true的。
    但是当你Integer g = 128时,也就是说不在-128到127这个静态化范围之外时,他会怎样呢?请看代码:
    public static Integer valueOf(int i) {
        final int offset = 128;
        if (i >= -128 && i <= 127) {
            return IntegerCache.cache[i + offset];
        }
            return new Integer(i);
        }
    它实际上是return new Integer(i),它new了一个对象,我们知道Integer是引用类型,在里面重新new了一次,等于又会重新返回一个引用,所以地址也被改变了。
    举个简单的例子:
    Integer h=128;Integer i=128;system.out.println(h==i);这个会为true吗?肯定不对,因为它们在里面重新new了一次,等于又会重新返回一个引用,所以地址被改变了。也就是说这是两个不同的引用,两个不同的地址。表达能力不行,不知道这样大家明不明白。
      

  5.   

    你也可以参考一下这个贴子.
    http://topic.csdn.net/u/20091022/08/0d3c88b3-1dea-4d47-836c-be4bb1537bba.html
    里面写得很详细.
      

  6.   

    Integer占了八个位.二进制01111111就等于128,如果再加1的话,就是11111111,而第一位是符号位.所以变成负的了.
      

  7.   

    说错了.
    这里有详细的说明
    http://topic.csdn.net/u/20090424/14/332bf1ba-6d26-4f71-91f5-402009d89464.html
      

  8.   

    基本数据类型的数据时由内存分配字节的,对于int,内存分配4字节,每字节由8位组成,4字节占32位。这样,它在内存中的存储范围就是-2的31次方到2的31次方减1,即-128到127(之所以数据部分时31位,是因为第一位是符号位,表示正负号)