Integer自动缓存池大小

来源:互联网 发布:网上数据库参考文献 编辑:程序博客网 时间:2024/05/16 07:02

之前一直看资料有写,当一个int类型的数据<128时,用==比较是相等的,但是当>128时,比较就不相等了。

<span style="font-size:18px;"><span style="font-family:Microsoft YaHei;font-size:14px;"><span style="font-family:KaiTi_GB2312;">public class Test {public static void main(String[]args ){          int i = 128;        Integer i2 = 128;        Integer i3 = new Integer(128);        //Integer会自动拆箱为int,所以为true        System.out.println(i == i2);//true        System.out.println(i == i3);//true          System.out.println(i2 == i3);//false  堆对象和栈对象,对象之间比较,而自动拆箱后对象都转换为int比较值        System.out.println("----------------------");        Integer i5 = 127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);        Integer i6 = 127;        System.out.println(i5 == i6);//true//        Integer i51 = 128;//        Integer i61 = 128;//        System.out.println(i51 == i61);//false         Integer i52=new Integer(126);        Integer i62=new Integer(126);        System.out.println(i52==i62);//false  堆中两个不同对象        System.out.println("----------------------");        Integer ii5 = new Integer(127);        System.out.println(i5 == ii5); //false  堆对象和栈对象,对象之间比较,        Integer i7 = new Integer(128);        Integer i8 = new Integer(128);        System.out.println(i7 == i8);  //false  <span style="line-height: 21.6px; font-family: tahoma, arial, sans-serif;">堆中两个不同对象</span>    }}</span></span></span>

上面是一个例子,大家可以自己运行一下。之前一直不理解为什么?今天看了源码才清楚。

下面把源码贴出来,大家感受一下,@@@@@

<span style="font-size:18px;"><span style="font-family:Microsoft YaHei;font-size:14px;"><span style="font-family:KaiTi_GB2312;font-size:14px;">private static class IntegerCache {       //这是默认的最小值,这个不可以改变       static final int low = -128;        static final int high;        static final Integer cache[];        static {            // 源码中写这个是一个可以手动设置的值,但是默认是127            int h = 127;//通过属性取值            String integerCacheHighPropValue =                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");            if (integerCacheHighPropValue != null) {                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);            }            high = h;            cache = new Integer[(high - low) + 1];            int j = low;            for(int k = 0; k < cache.length; k++)                cache[k] = new Integer(j++);        }</span></span></span>

   我们可以看到,integer的自动缓存池的大小是 [-128   127] 这也是为什么就我们的环境运行出来,大于127就不再相等。

通过属性去取的high的值,sun.misc.VM.getSavedProperty,

这个属性是在使用Oracle/Sun JDK 6,在server模式下,使用-XX:AutoBoxCacheMax=NNN参数即可将Integer的自动缓存区间设置为[-128,NNN]。注意区间的下界固定在-128不可配置。 
在client模式下该参数无效。这个参数是server模式专有的,在c2_globals.hpp中声明,默认值是128;不过这个默认值在默认条件下不起作用,要手动设置它的值或者是开启-XX:+AggressiveOpts参数才起作用。 


那么在这里缓存池到底起到什么作用?

Integer i5 = 127时,会将127进行缓存,下次再写Integer i6 = 127时,就会直接从缓存中取,就不会new了。所以22行的结果为true,而25行为false。(自动装箱时才会发生,Integer a=value,这种栈对象形式(类似 string str="abc"),如果Integer b= new Integer(value),则不会,分别在堆中建立对象)

 

对于23行和26行,因为对象不一样,所以为false。

 

对于以上的情况总结如下:

 

  ①无论如何,Integer与new Integer不会相等(栈对象,堆对象)。不会经历拆箱过程,i3的引用指向堆,而i4指向专门存放他的内存(常量池),他们的内存地址不一样,所以为false。

  ②两个都是非new出来的Integer(两个栈对象,涉及自动装箱),如果数在-128到127之间,则是true,否则为false

  java在编译Integer i2 = 128的时候,被翻译成-> Integer i2 = Integer.valueOf(128);而valueOf()函数会对-128到127之间的数进行缓存。

  ③两个都是new出来的,都为false(两个堆对象)。

  ④int和integer(无论new否)比(Integer涉及自动拆箱),都为true,因为会把Integer自动拆箱为int再去比。



0 0
原创粉丝点击