- 浏览: 97710 次
- 性别:
- 来自: 广州
文章分类
最新评论
一、引例,看如下代码
二、究竟哪个范围的整型数被缓存而不需要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选项,调整“自动装箱池”的大小
2、IntegerCache源码
看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选项,调整“自动装箱池”的大小
-------------------以下为补充内容------------------
最近在看Long类,顺便贴个LongCache来比较,相对Integer简单很多,就像ByteCache一样。
int a = 100, b = 100; System.out.println(a == b); // true,缓存了 Integer c = 1000, d = 1000; System.out.println(c == d); // false,没有缓存,要new Integer e = -128, f = -128; System.out.println(e == f); // true,缓存了 Integer g = -129, h = -129; 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选项,调整“自动装箱池”的大小
// value of java.lang.Integer.IntegerCache.high property (obtained during VM init) private static String integerCacheHighPropValue; static void getAndRemoveCacheProperties() { if (!sun.misc.VM.isBooted()) { Properties props = System.getProperties(); integerCacheHighPropValue = (String)props.remove("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) System.setProperties(props); // remove from system props } }
2、IntegerCache源码
// IntegerCache,一个内部类,注意它的属性都是定义为static final private static class IntegerCache { static final int high; //缓存上界,暂为null static final Integer cache[]; //缓存的整型数组 // 块,为什么定义为块 static { final int low = -128; // 缓存下界,不可变了。只有上界可以改变 // high value may be configured by property // h值,可以通过设置jdk的AutoBoxCacheMax参数调整(以下有解释),自动缓存区间设置为[-128,N]。注意区间的下界是固定 int h = 127; if (integerCacheHighPropValue != null) { // Use Long.decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized // 通过解码integerCacheHighPropValue,而得到一个候选的上界值 int i = Long.decode(integerCacheHighPropValue).intValue(); // 取较大的作为上界,但又不能大于Integer的边界MAX_VALUE i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - -low); } high = h; //上界确定 // 就可以创建缓存块,注意缓存数组大小 cache = new Integer[(high - low) + 1]; // int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // -128到high值逐一分配到缓存数组 } private IntegerCache() {} }
看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选项,调整“自动装箱池”的大小
// value of java.lang.Integer.IntegerCache.high property (obtained during VM init) private static String integerCacheHighPropValue; static void getAndRemoveCacheProperties() { if (!sun.misc.VM.isBooted()) { Properties props = System.getProperties(); integerCacheHighPropValue = (String)props.remove("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) System.setProperties(props); // remove from system props } }
-------------------以下为补充内容------------------
最近在看Long类,顺便贴个LongCache来比较,相对Integer简单很多,就像ByteCache一样。
private static class LongCache { private LongCache(){} // 可见下界和上界就固定了,-128到127 static final Long cache[] = new Long[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Long(i - 128); } } public static Long valueOf(long l) { final int offset = 128; if (l >= -128 && l <= 127) { // will cache return LongCache.cache[(int)l + offset]; } return new Long(l); }
发表评论
-
Enum类源码浅析
2012-08-27 17:20 0摘录出处:http://blog.csdn.net/abing ... -
Float源码浅析equals和compare
2012-08-27 15:53 905public boolean equals(Object o ... -
Integer源码浅析getInteger(String nm,Integer val)
2012-08-24 11:40 2056一、根据nm(key)参数,通过System.getPrope ... -
Integer.parseInt(String s ,int radix)方法使用解释
2012-08-22 17:27 7424方法parseInt(String s,int radix)的 ... -
Integer源码浅析toString(int i,int radix)
2012-08-22 16:23 1258前言,大概了解了Integer的内部,而至于Long,就同理可 ... -
Character源码浅析未完成
2012-08-24 15:54 1155一、Character的缓存设计 private fina ... -
StringBuffer源码浅析(insert方法)
2012-08-11 11:35 3968一、insert,其实跟replace差不多,都是先通过Sys ... -
StringBuffer源码浅析(replace与substring方法)
2012-08-10 14:34 4104一、replace方法也比较简单。围观源码 public ... -
StringBuffer源码浅析(append方法)
2012-08-09 10:17 977一、append(int i),来自StringBuffer的 ... -
StringBuffer源码浅析(大致结构和append方法)
2012-07-31 17:28 9750StringBuffer类继承自AbstractStringB ...
相关推荐
主要介绍了Integer IntegerCache源码阅读,具有一定借鉴价值,需要的朋友可以参考下
nodemcu_integer_master_20200312-1329_nodemcu-master_nodemcusmartconfig_nodemcumaster_nodemcu_nodemcu-integer_源码.rar
主要给大家介绍了关于Java 9中如何对IntegerCache进行修改的相关资料,文中通过示例代码介绍的非常详细,对大家学习或使用java9具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
java integer 源码 Free Code Camp
主要介绍了Java源码解析Integer方法解读,包括toString方法、toUnsignedString方法、highestOneBit方法等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Transform Coding with Integer-to-Integer Transforms.pdf H.264學習資料
VB编写的仿Windows扫雷游戏,和系统自带的界面相似,扫雷QQ版VB源码,有什么更好的想法请联系QQ:149550122。Dim bomb_number As Integer '地雷数 Dim flag(63) As Integer '地雷数组 flag=1为右地雷 flag=0为没有...
HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For ...
Clock_Integer 指令库应用例程rar,Clock_Integer 指令库应用例程
HugeInteger.java
用于RAR密码暴力破解的源码-仅供参考 function ExtractArchive(ArcName: PChar; Mode: Integer):boolean; var hArcData: THandle; RHCode, PFCode: Integer; CmtBuf: array[0..Pred(16384)] of Char; HeaderData...
bigdecimal转integer
mybatis返回int会报错,改成Integer封装类型可以解决,具体参考我的博客
U盘只读程序源码源码 DELPHI版 function InstallService(SvcName, SvcDispName, SvcFullPath: string): integer; var hSCM, hService: SC_HANDLE; sInfo: string; begin result := 0; hSCM := OpenSCManager...
Gaussian Integer
integer原始码二进制搜索 该存储库包含Java的源代码,该源代码用于使用二进制搜索算法在数组中搜索整数的算法。 可以很容易地转换代码以在子集中查找特定项,现在,它在子集0到n(基本上是整个数组)中寻找该项。 ...
由于使用模板,所以只有一个Integer.h头文件 支持如下操作:>, >=, <, , ==, !=, +, +=, -, -=, *, =, /, /=,(输出);支持利用基本整数类型(int,unsigned,long long,……)和字符串(char和std::string)构造类型
Logic and Integer Programming
创建一个大整数类HugeInteger,该类用一个40个元素的数组来存放一个大整数(最多不超过40位)。 构造函数原型: HugeInteger(String); (1)定义几个大整数算术运算的成员函数,包括input、output、add和sub, add, ...
C++实现的大整数(big integer)类的源代码。