有关整数缓冲区的问题
问题:
- 用构造函数装箱,比较结果为false;
- 采用自动装箱,100时比较结果为true,200时结果比较为false。
- 问题实例代码:
public class IntegerLei {
public static void main(String[] args) {
// 1.用构造函数装箱
Integer integer1 = new Integer(100);
Integer integer2 = new Integer(100);
System.out.println(integer1==integer2);
// 2.用自动装箱
Integer integer3 = 100;
Integer integer4 = 100;
System.out.println(integer3==integer4);
integer3 = 200;
integer4=200;
System.out.println(integer3==integer4);
}
}
/*运行结果:
false
true
false
*/
原因:
-
采用XJad软件我们可以把编译好的.class文件反编译回java文件,可以发现自动装箱操作其实是采用了Integer.valueof()方法;
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://kpdus.tripod.com/jad.html // Decompiler options: packimports(3) fieldsfirst ansi space // Source File Name: IntegerLei.java import java.io.PrintStream; public class IntegerLei { public IntegerLei() { } public static void main(String args[]) { Integer integer1 = new Integer(100); Integer integer2 = new Integer(100); System.out.println(integer1 == integer2); Integer integer3 = Integer.valueOf(100); Integer integer4 = Integer.valueOf(100); System.out.println(integer3 == integer4); integer3 = Integer.valueOf(200); integer4 = Integer.valueOf(200); System.out.println(integer3 == integer4); } }
-
so:问题就出现valueof的源码。源码如下:
@IntrinsicCandidate public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
发现:valueOf(int i)方法中的参数值在
IntegerCache.low
与IntegerCache.high
之间时,会返回数组IntegerCache.cache[]
中的值。为查看三者的值查看
IntegerCache
类的源码如下:private static class IntegerCache { static final int low = -128; static final int high; static final Integer[] cache; static Integer[] archivedCache; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { h = Math.max(parseInt(integerCacheHighPropValue), 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(h, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; // Load IntegerCache.archivedCache from archive, if possible CDS.initializeFromArchive(IntegerCache.class); int size = (high - low) + 1; // Use the archived cache if it exists and is large enough if (archivedCache == null || size > archivedCache.length) { Integer[] c = new Integer[size]; int j = low; for(int i = 0; i < c.length; i++) { c[i] = new Integer(j++); } archivedCache = c; } cache = archivedCache; // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} }
从源码中可以看出
IntegerCache.low==-128
、IntegerCache.high==127
,同时可以看出数组cache[]
大小为256,存放的元素为{-128,…,127}。 -
回到valueof的源码,当
valueOf(int i)
中的整数值在[-128,127]之间,返回的是数组cache[]中的元素,当超过这个范围,调用new Integer()
构造函数进行装箱。
XJad反编译软件下载链接
XJad-class反编译工具 https://www.aliyundrive.com/s/S9PZ1G7iWti 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。