Integer源码浅析IntegerCache

一、引例,看如下代码 
Java代码   收藏代码
  1. int a = 100, b = 100;  
  2. System.out.println(a == b); // true,缓存了  
  3. Integer c = 1000, d = 1000;  
  4. System.out.println(c == d); // false,没有缓存,要new  
  5. Integer e = -128, f = -128;  
  6. System.out.println(e == f); // true,缓存了  
  7. Integer g = -129, h = -129;  
  8. System.out.println(g == h); // false,没有缓存,要new  


二、究竟哪个范围的整型数被缓存而不需要new了呢?先了解一下什么是“自动装箱池”,再围观IntegerCache源码就清楚。 

1、看API里面的注释 
    /** 
     * 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. During VM initialization the 
     * getAndRemoveCacheProperties method may be used to get and remove any system 
     * properites that configure the cache size. At this time, the size of the 
     * cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>. 
     */ 
getAndRemoveCacheProperties方法,用于获取或移除JDK对Integer设置的缓存属性,同时也是调整jvm:AutoBoxCacheMax选项,调整“自动装箱池”的大小 
Java代码   收藏代码
  1. // value of java.lang.Integer.IntegerCache.high property (obtained during VM init)  
  2.   private static String integerCacheHighPropValue;   
  3.   
  4.   static void getAndRemoveCacheProperties() {  
  5.       if (!sun.misc.VM.isBooted()) {  
  6.           Properties props = System.getProperties();  
  7.           integerCacheHighPropValue =  
  8.               (String)props.remove("java.lang.Integer.IntegerCache.high");  
  9.           if (integerCacheHighPropValue != null)  
  10.               System.setProperties(props);  // remove from system props  
  11.       }  
  12.   }  


2、IntegerCache源码 
Java代码   收藏代码
  1. // IntegerCache,一个内部类,注意它的属性都是定义为static final  
  2. private static class IntegerCache {  
  3.     static final int high; //缓存上界,暂为null  
  4.     static final Integer cache[]; //缓存的整型数组  
  5.       
  6.     // 块,为什么定义为块  
  7.     static {  
  8.         final int low = -128// 缓存下界,不可变了。只有上界可以改变  
  9.   
  10.         // high value may be configured by property  
  11.         // h值,可以通过设置jdk的AutoBoxCacheMax参数调整(以下有解释),自动缓存区间设置为[-128,N]。注意区间的下界是固定  
  12.         int h = 127;  
  13.           
  14.         if (integerCacheHighPropValue != null) {  
  15.             // Use Long.decode here to avoid invoking methods that  
  16.             // require Integer's autoboxing cache to be initialized  
  17.             // 通过解码integerCacheHighPropValue,而得到一个候选的上界值  
  18.             int i = Long.decode(integerCacheHighPropValue).intValue();  
  19.             // 取较大的作为上界,但又不能大于Integer的边界MAX_VALUE  
  20.             i = Math.max(i, 127);       
  21.             // Maximum array size is Integer.MAX_VALUE  
  22.             h = Math.min(i, Integer.MAX_VALUE - -low);  
  23.         }  
  24.         high = h; //上界确定  
  25.         // 就可以创建缓存块,注意缓存数组大小  
  26.         cache = new Integer[(high - low) + 1]; //  
  27.         int j = low;  
  28.         for(int k = 0; k < cache.length; k++)  
  29.             cache[k] = new Integer(j++); // -128到high值逐一分配到缓存数组  
  30.     }  
  31.   
  32.     private IntegerCache() {}  
  33. }  



看API里面的注释 
    /** 
     * 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. During VM initialization the 
     * getAndRemoveCacheProperties method may be used to get and remove any system 
     * properites that configure the cache size. At this time, the size of the 
     * cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>. 
     */ 
getAndRemoveCacheProperties方法,用于获取或移除JDK对Integer设置的缓存属性,同时也是调整jvm:AutoBoxCacheMax选项,调整“自动装箱池”的大小 
Java代码   收藏代码
  1. // value of java.lang.Integer.IntegerCache.high property (obtained during VM init)  
  2.   private static String integerCacheHighPropValue;   
  3.   
  4.   static void getAndRemoveCacheProperties() {  
  5.       if (!sun.misc.VM.isBooted()) {  
  6.           Properties props = System.getProperties();  
  7.           integerCacheHighPropValue =  
  8.               (String)props.remove("java.lang.Integer.IntegerCache.high");  
  9.           if (integerCacheHighPropValue != null)  
  10.               System.setProperties(props);  // remove from system props  
  11.       }  
  12.   }  


-------------------以下为补充内容------------------
 
最近在看Long类,顺便贴个LongCache来比较,相对Integer简单很多,就像ByteCache一样。 
Java代码   收藏代码
  1. private static class LongCache {  
  2. private LongCache(){}  
  3.        // 可见下界和上界就固定了,-128到127  
  4. static final Long cache[] = new Long[-(-128) + 127 + 1];  
  5.   
  6. static {  
  7.     for(int i = 0; i < cache.length; i++)  
  8.     cache[i] = new Long(i - 128);  
  9. }  
  10.    }  
  11.   
  12. public static Long valueOf(long l) {  
  13. final int offset = 128;  
  14. if (l >= -128 && l <= 127) { // will cache  
  15.     return LongCache.cache[(int)l + offset];  
  16. }  
  17.        return new Long(l);  
  18.    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值