前几天推送过一篇关于Integer比较大小的文章,具体可以查看
《【细节】Integer细节比较》,有朋友提到说,Integer是有小数据缓存的机制的,那么第一个是否应该是true呢?
回归下第一个:
1 |
Integer num1 = new Integer( 100 ); |
2 |
Integer num2 = new Integer( 100 ); |
3 |
System.out.println(num1 == num2); |
上篇提到说,这个的运行结果是false,原因在上篇文章中有解释,但是,对于Integer的小数据缓存是怎么一回事?难道这两个是矛盾的吗?
PS:灰常感谢朋友的提出Integer缓存问题(之前不知道有这个机制)瞬间涨知识了。
言归正传,
那么Integer缓存是怎么回事呢?
还是用代码的方式来解决问题吧,下面是代码,同样,你觉得输出结果会是什么?
01 |
public static void main(String[] args) { |
03 |
Integer num1 = new Integer( 100 ); |
04 |
Integer num2 = new Integer( 100 ); |
05 |
System.out.println( "num1==num2 " + (num1 == num2)); |
09 |
System.out.println( "num3==num4 " +(num3 == num4)); |
13 |
System.out.println( "num5==num6 " + (num5 == num6)); |
16 |
Integer num8 = new Integer( 100 ); |
17 |
System.out.println( "num7==num8 " + (num7 == num8)); |
20 |
Integer num10 = new Integer( 100 ); |
22 |
System.out.println( "num9==num10 " + (num9 == num10)); |
23 |
System.out.println( "num9==num11 " + (num9 == num11)); |
运行之后的输出结果是:
num1==num2 false num3==num4 true num5==num6 false num7==num8 false num9==num10 true
num9==num11 true
|
解释一下(个人理解,如果觉得有偏差,请联系我(公众号回复即可)~~)
num1==num2 false
这个上次的文章中已经解释过了,num1和num2的内存地址不一样,==的左右操作数如果是对象的话,那么比较的是引用的地址,new产生的对象一定是新的内存地址,所以,这里和Integer的缓存机制无关的,最终的结果便是false
num3==num4 true
这里是true,num3和num4的初始化方式,是直接将数字赋值,这种方式,在初始化的时候,等价与下面的代码:
Integer num3 = Integer.valueOf(100);
这里便涉及到了Integer的缓存机制问题, Integer是对小数据(-128~127)是有缓存的,再jvm初始化的时候,数据-128~127之间的数字便被缓存到了本地内存中,这样,如果初始化-128~127之间的数字,便会直接从内存中取出,而不需要再新建一个对象(后面会分析valueOf的代码和缓存的一点事情),所以,100这个数字再-128~127之间,那么num3和num4实际上是引用的是一个内存地址,那么自然就是true了。
num5==num6 false
同上解析,128已经不在-128~127之间了,所以会使用new新建个对象,那么num5和num6的内存地址就不一样了,结果就是false
num7==num8 false
这个和第一个的解释基本一致,因为num8是new出来的,所以使用的内存地址和num7不一致,结果为false
num9==num10 true
这个就比较好玩了,这个和上面的那个唯一的区别在于num9的类型是int而num7的类型是Integer,那么为啥这里是true呢?
Integer是int的包装类,在和int做比较的时候,会自动拆箱成int数值类型,所以,这里便变成了数字(int)的比较,so。。100肯定等于100啦~
num9==num11 true
解析同上(不需要关注到底是否是new出来,因为最终都要拆箱,最终都要将比较变成基本数据类型的比较)
So~~ 不知道是否讲的清楚了~~有疑问,随时骚扰~~
附:关于Integer的缓存的源代码分析。
首先是valueOf的代码分析:
1 |
public static Integer valueOf( int i) { |
2 |
assert IntegerCache.high >= 127 ; |
3 |
if (i >= IntegerCache.low && i <= IntegerCache.high) |
4 |
return IntegerCache.cache[i + (-IntegerCache.low)]; |
可以看到,在执行valueOf的时候,会先去检查内存中是否存在该数字,如果存在的话,就直接从内存中取出返回,如果不存在的话,就新建一个Integer对象。
(PS:这里有个assert的使用哦~~后面解释一下这里)
这里涉及到了一个IntegerCache.high的数值~~,让我们看下它是怎么初始化的
01 |
private static class IntegerCache { |
02 |
static final int low = - 128 ; |
03 |
static final int high; |
04 |
static final Integer cache[]; |
08 |
String integerCacheHighPropValue = |
09 |
sun.misc.VM.getSavedProperty( "java.lang.Integer.IntegerCache.high" ); |
10 |
if (integerCacheHighPropValue != null ) { |
11 |
int i = parseInt(integerCacheHighPropValue); |
14 |
h = Math.min(i, Integer.MAX_VALUE - (-low)); |
17 |
cache = new Integer[(high - low) + 1 ]; |
19 |
for ( int k = 0 ; k < cache.length; k++) |
20 |
cache[k] = new Integer(j++); |
22 |
private IntegerCache() {} |
可以看到,hign的值,默认是127, 如果
java.lang.Integer.IntegerCache.high
的变量有设置,则取这个值。jvm再初始化的时候,会将低值(-128)到高值(默认127)之间的数字加载到内存中。
不知道有没有注意到: 低值是固定的,不能改变,只能是-128,但是高值是可以通过jvm参数改变的~
So,如果业务场景下,需要将高值变更怎么设置呢?
在java程序执行的时候加上
-XX:AutoBoxCacheMax= 的参数即可~~ 可以尝试下设置成200,那么上面的demo栗子的中的结果会变化哦~~
BTW
上面设计到了一个assert,java里面的断言,一般程序中,我们在执行的时候,默认是关闭断言的,valueOf方法里面的第一句断言,正常情况下一定是true,所以后面的代码逻辑一定是会执行的,但是,如果jvm初始化失败,或者是其他原因的话,便会抛出断言异常(前提是开启了断言功能,jvm启动参数上添加-ea即可)
其他包装类型也是有缓存机制的~~不妨看下~~
(这里不赘述其他包装类型的缓存机制了,感兴趣的不妨看下JDK源代码)
原文地址: http://www.dutycode.com/xijie_baozhuanglei_huancun_jizhi_integer_huancun.html
如果喜欢可以关注我的公众号~~