涉及到的源码是 Java 1.8 的
有如下一段代码(摘抄自《深入理解 Java 虚拟机-第 2 版》P316):
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d); // true
System.out.println(e == f); // false
System.out.println(c == (a + b)); // true
System.out.println(c.equals((a + b))); // true
System.out.println(g == (a + b)); // true
System.out.println(g.equals(a + b)); // false
其运行结果如注释。
上述代码在编译之后如下:
Integer a = Integer.valueOf(1);
Integer b = Integer.valueOf(2);
Integer c = Integer.valueOf(3);
Integer d = Integer.valueOf(3);
Integer e = Integer.valueOf(321);
Integer f = Integer.valueOf(321);
Long g = Long.valueOf(3L);
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c.intValue() == a.intValue() + b.intValue());
System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
System.out.println(g.longValue() == a.intValue() + b.intValue());
System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
对于答案的推理,主要是有几个点:
1、自动装箱,Integer a = 1
实际上是调用 Integer.valueOf(1)
2、Integer 类中有一个静态的 Integer 数组,默认缓存了值在 [-128, 127]
之间对应的 Integer 对象(即常量池)。因此通过 Integer.valueOf()
方法时,如果在 [-128, 127]
之间则是直接取的缓存对象。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
当然,如果是直接 new 的,则会生产一个新的对象。
3、对于包装类,“==”
在不遇到算数运算的情况下不会自动拆箱,因此此时是看是不是同一个对象。换句话说,当 “==”
遇到遇到算数运算时,就会自动拆箱,转换为对比值是否相等。
4、equals()
方法不处理数据转型的关系。(equlas 比较的是对象)
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
比如 Long 的 equals() 方法,先会判断传递进来的 obj 是不是 Long 类型,是的话才会对比值是否相等。
补充:
1、有关 Integer 的常量池范围,是通过如下代码实现的:
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 (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
范围的左边界是固定的,即 -128,但是右边界默认是 127,后来在 Java 6 中,最大值映射到 java.lang.Integer.IntegerCache.high
,可以使用 JVM 的启动参数设置最大值。这使我们可以根据应用程序的实际情况灵活地调整来提高性能。但是也会保证 high 不会超过 Integer.MAX_VALUE - 129
,从而保证缓存数组的长度最大为 Integer.MAX_VALUE
。
是什么原因选择这个 -128 到 127 这个范围呢?
因为这个范围的整数值是使用最广泛的。 在程序中第一次使用 Integer 的时候也需要一定的额外时间来初始化这个缓存。
2、关于其他几个包装类型的常量池,有:
Short、Long、Byte 范围在 [-128, 127];
Character 则缓存了 ASCII 对应的 128 个字符;
Boolean 有 true、false 对应的两个常量;
Float、Double 则没有。因为对于浮点数,小数点后面可以无限精确,不好建立某一范围对应的常量池。
3、String 类和常量池
截图自:Java常量池理解与总结
4、常量池主要用于存放两大类常量:字面量 (Literal) 和符号引用量 (Symbolic References),字面量相当于 Java 语言层面常量的概念,如文本字符串,声明为 final 的常量值等,符号引用则属于编译原理方面的概念,包括了如下三种类型的常量:
类和接口的全限定名
字段名称和描述符
方法名称和描述符
摘抄自:Java常量池理解与总结