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); }
看源码就知道了,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); }
下面是源码: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了一次,等于又会重新返回一个引用,所以地址被改变了。也就是说这是两个不同的引用,两个不同的地址。表达能力不行,不知道这样大家明不明白。
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);
}
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);
}
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了一次,等于又会重新返回一个引用,所以地址被改变了。也就是说这是两个不同的引用,两个不同的地址。表达能力不行,不知道这样大家明不明白。
http://topic.csdn.net/u/20091022/08/0d3c88b3-1dea-4d47-836c-be4bb1537bba.html
里面写得很详细.
这里有详细的说明
http://topic.csdn.net/u/20090424/14/332bf1ba-6d26-4f71-91f5-402009d89464.html