昨天说到两个值是128的 Integer 对象用 == 来比较的话结果是 false,今天解释下为什么
== 原理
看了昨天的文章的朋友应该明白,== 其实是对内存地址的比较,对于这段结果为 false 的代码来说,
public class Demo { public static void main(String[] args) { Integer a = 128; Integer b = 128; System.out.println("result: " + (a == b)); }}
false的结果只有一个可能性,就是两个的内存地址不一样。
现在我们来稍微修改一下 Integer的值,变成127再看看,
public class Demo { public static void main(String[] args) { Integer a = 127; Integer b = 127; System.out.println("result: " + (a == b)); }}
$ java Demoresult: true
晕了么,为啥这里的内存地址又一样了?
Integer对数据的处理逻辑
== 在对基本类型进行比较的时候一切都还好,还能按照预期的结果运行,但如果对复合类型的话就有点不一样了。
对于 Integer来说,范围在 -128 ~ 127 的数,在内存中会有个缓冲数组用,存了对应的数,每次使用的时候只是从其中拿对应的 Integer对象出来复用而已,所以 == 对于在这个范围的 Integer来说,只要值相同,内存地址就是相同的。
而超出这个范围的话,当然内存地址也就不同了。
如果你看过 Integer的源码就明白了,贴一下简单的源码,
/** * 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的缓存对象了如果我们挖深一层,看看在初始化 Integer对象的时候都发生了什么,就更能明白其中的逻辑了。比如上面那段 127 的例子代码,我们把它的字节码拿出来看一下,
public class Demo { public Demo(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: bipush 127 2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 5: astore_1 6: bipush 127 8: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 11: astore_2....}
这里很明显可以看到,Integer a = 127,其实编译器是调用了 Integer.valueOf(127)来实现,而 valueOf() 的逻辑是,
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);}
看到了么,如果落在 IntegerCache.low 到 IntegerCache.high 范围的数,是直接从 cache中拿的。
总结
对于 Integer a = %d 来说,在 -128 ~ 127 范围的数的比较,== 没什么问题,因为缓存池的存在,这里比较的是相同的内存地址,但当超过这个范围的话,用 == 来比较相同值的 Integer 对象是错误的,因为在这个范围外的 Integer 对象有独立的内存地址,只能用 equals()来比较。
当然如果在初始化 -128 ~ 127 范围的 Integer对象的时候,如果用的是 new Integer()构造方法的话,也会分配不同的内存对象,这时候 == 也是不行的。
所以在项目中为了谨慎起见,对于非基本类型外的对象,尽量用 equals()来比较才是明智的选择。