首先看一段代码(使用JDK 5),如下:
public class Test {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
if (i1 == i2)
System.out.println("Equal!");
else
System.out.println("Not equal!");
}
}
输出结果想必大家也知道,是“Equal!”。现在把i1和i2的值由127改为128看看会发生什么?结果输出“Not equal!”。
这个问题的答案看了下面的就会清楚了。
为了更容易理解问题,用Jad将上面代码反编译,如下:
import java.io.PrintStream;
public class Test
{
public Test()
{
}
public static void main(String args[])
{
Integer i1 = Integer.valueOf(128);
Integer i2 = Integer.valueOf(128);
Integer i3 = Integer.valueOf(128);
if(i1 == i2)
System.out.println("i1 == i2 is true!");
else
System.out.println("i1 == i2 is false!");
if(i1.intValue() >= i2.intValue())
System.out.println("i1 >= i2 is true!");
else
System.out.println("i1 >= i2 is false!");
if(i1 == i3)
System.out.println("i1 == i3 is true!");
else
System.out.println("i1 == i3 is false!");
}
}
从这可以看出,“Integer i1 = 127;”在JDK1.5下应该编译成了“Integer i1 = Integer.valueOf(127);”。
再看看java.lang.Integer中关于valueOf的源代码是怎样的:
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset]; //这个主意下,Cache 缓存了-128~127的值
}
return new Integer(i);
}
可以看出,这个值在-128到127之间,会将其cached(缓存)起来,如果多次使用的话,会节省内存和改善性能;如果不在这个范围之内,则生成一个新的Integer Object instance,这样如果进行“==”时,由于是比较两个不同的Object references,故结果是false。事实上,这个特性从JDK 1.3就存在了(以前的我不清楚) 。
确切的说,上面出现的问题实质并不是Autoboxing和Auto-Unboxing,应该是Integer类的valueOf(int i)方法,只是Autoboxing和Auto-Unboxing将这个问题掩盖了。
把Integer 换成 int 则不会出现上述情况,结果都是equal。