java对象的比较运算符"=="是对象地址比较,但基本数据类型,如Byte, Boolean, Integer等基本类型的封装类在使用==时,有些细节需要注意,如以下代码:
public static void main(String []args) {
Integer i1 = 1;
Integer i2 = 1;
Integer i3 = new Integer(1);
System.out.println(i1 == i2); // true,i1 和 i2是一个对象
System.out.println(i1 == i3); // false,i1 和 i3 不是一个对象
}
为何i1和i2的地址一样呢, 我们可以看一下Integer的valueOf实现
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropVal