==运算符和equals()方法的区别、数字常量池IntegerCache
文章目录
==比较运算符:对象比较内存地址,基本数据类型比较值相等
==比较的是两个基本数据类型的值是否相等或者两个对象的引用地址是否一样
public static void main(String args[]) {
int a = 1000, b = 1000;
System.out.println(a == b);
Integer c = 1000, d = 1000;
System.out.println(c == d);
Integer e = 100, f = 100;
System.out.println(e == f);
}
运行的结果为:true false true
原因:
-
a和b都是基本数据类型,值也相等,所以 a==b 为true
-
Integer c = 1000 是将一个基本数据类型的值赋给对象的引用。这里涉及到了装箱的概念,就是把基本类型用它们相应的引用类型包装起来,使其具有对象的性质。
编译器用的是public static Integer valueOf(int i)方法。
当i的值在[-128,127]之间时,返回的是IntegerCache缓存的对象的已用,否则返回的是新的对象的引用。
因此,c 和 d 是两个不同的对象, e 和 f 是两个相同的对象。
通过调试也可以看到他们的id值也说明了这一点。
equals()方法
Object.equals()方法源码
public boolean equals(Object obj) {
return (this == obj);
}
可以看出equals()就是使用的==运算符,但是区别在于,equals方法可以被子类重写,以实现不同的比较效果
重写了equals的类
统计了下java.Lang目录(不计算子目录)重写了equals方法的类有15个
java.lang (15 usages found)
Boolean (1 usage found)
225 public boolean equals(Object obj) {
Byte (1 usage found)
419 public boolean equals(Object obj) {
Character (1 usage found)
4619 public boolean equals(Object obj) {
Character.Subset (1 usage found)
613 public final boolean equals(Object obj) {
Double (1 usage found)
797 public boolean equals(Object obj) {
Enum (1 usage found)
142 public final boolean equals(Object other) {
Float (1 usage found)
707 public boolean equals(Object obj) {
Integer (1 usage found)
973 public boolean equals(Object obj) {
Long (1 usage found)
1074 public boolean equals(Object obj) {
ProcessBuilder.Redirect (1 usage found)
638 public boolean equals(Object obj) {
ProcessEnvironment.CheckedEntry (1 usage found)
127 public boolean equals(Object o) {return e.equals(o);}
Short (1 usage found)
424 public boolean equals(Object obj) {
StackTraceElement (1 usage found)
198 public boolean equals(Object obj) {
String (1 usage found)
976 public boolean equals(Object anObject) {
Thread.WeakClassKey (1 usage found)
2010 public boolean equals(Object obj) {
遇见这些类对象的时候,不能继续使用==的判断方法,需要注意其equals方法的具体实现,这15个类晚点整理出来再写一个文档//TODO
IntegerCache数字常量池来龙去脉
[public static Integer valueOf(int i)源码分析] {#1}
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
IntegerCache.cache[i + (-IntegerCache.low)]
数组index从0开始,所以减去IntegerCache.low
IntegerCache类
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
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() {}
}
上述代码指出了,IntegerCache对象的默认范围是[-128~127],最大值可以在jvm启动时通过参数指定
所以说常量池就是个数组
注意:
- h = Math.min(i, Integer.MAX_VALUE - (-low) -1)
这一句求min的原因出于安全考虑,只有i大于127并且小于Integer.MAX_VALUE才不超过Integer的范围- Integer.MAX_VALUE - (-low) -1
写上这个表达式的原因是MAX_VALUE是Integer的最大范围,但是,这里还有-128的下限,何在一起总范
围就是MAX_VALUE+128–>溢出
所以,需要减去下限的值,确保最终[low,high]的范围不溢出
TODO:
上述15个equals相关类和compareTo方法晚点一起整理出来,写一份文档