自动拆装箱-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缓存是怎么回事呢?

还是用代码的方式来解决问题吧,下面是代码,同样,你觉得输出结果会是什么?

 
  1. public static void main(String[] args) {

  2.     Integer num1 = new Integer(100);

  3.     Integer num2 = new Integer(100);

  4.     System.out.println("num1==num2 " + (num1 == num2));

  5.      

  6.     Integer num3 = 100;

  7.     Integer num4 = 100;

  8.     System.out.println("num3==num4 " +(num3 == num4));

  9.      

  10.     Integer num5 = 128;

  11.     Integer num6 = 128;

  12.     System.out.println("num5==num6 " + (num5 == num6));

  13.      

  14.     Integer num7 = 100;

  15.     Integer num8 = new Integer(100);

  16.     System.out.println("num7==num8 " + (num7 == num8));

  17.      

  18.     int num9 = 100;

  19.     Integer num10 = new Integer(100);

  20.     Integer num11 = 100;

  21.     System.out.println("num9==num10 " + (num9 == num10));

  22.     System.out.println("num9==num11 " + (num9 == num11));

  23.  
  24. }


运行之后的输出结果是:

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出来,因为最终都要拆箱,最终都要将比较变成基本数据类型的比较)

附:关于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)];

  5. return new Integer(i);

  6. }

可以看到,在执行valueOf的时候,会先去检查内存中是否存在该数字,如果存在的话,就直接从内存中取出返回,如果不存在的话,就新建一个Integer对象。

这里涉及到了一个IntegerCache.high的数值~~,让我们看下它是怎么初始化的

 
  1. private static class IntegerCache {

  2.     static final int low = -128;

  3.     static final int high;

  4.     static final Integer cache[];

  5.     static {

  6.         // high value may be configured by property

  7.         int h = 127;

  8.         String integerCacheHighPropValue =

  9.             sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

  10.         if (integerCacheHighPropValue != null) {

  11.             int i = parseInt(integerCacheHighPropValue);

  12.             i = Math.max(i, 127);

  13.             // Maximum array size is Integer.MAX_VALUE

  14.             h = Math.min(i, Integer.MAX_VALUE - (-low));

  15.         }

  16.         high = h;

  17.         cache = new Integer[(high - low) + 1];

  18.         int j = low;

  19.         for(int k = 0; k < cache.length; k++)

  20.             cache[k] = new Integer(j++);

  21.     }

  22.     private IntegerCache() {}

  23. }

可以看到,hign的值,默认是127, 如果java.lang.Integer.IntegerCache.high的变量有设置,则取这个值。jvm再初始化的时候,会将低值(-128)到高值(默认127)之间的数字加载到内存中。

不知道有没有注意到: 低值是固定的,不能改变,只能是-128,但是高值是可以通过jvm参数改变的~

So,如果业务场景下,需要将高值变更怎么设置呢?

在java程序执行的时候加上 -XX:AutoBoxCacheMax=<size> 的参数即可~~ 可以尝试下设置成200,那么上面的demo栗子的中的结果会变化哦~~

其他包装类型也是有缓存机制的~~不妨看下~~

from: https://blog.csdn.net/YingHuaNanHai/article/details/80735821

猜你喜欢

转载自blog.csdn.net/GarfieldEr007/article/details/84729564