Integer 中的缓存类IntegerCache
2016-12-19 10:07
288 查看
Cache为[-128,127],IntegerCache有一个静态的Integer数组,在类加载时就将-128 到 127 的Integer对象创建了,并保存在cache数组中,一旦程序调用valueOf 方法,如果取的值是在-128 到 127 之间就直接在cache缓存数组中去取Integer对象,超出范围就new一个对象。
private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property 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)); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} }
Integer i = 127; Integer j = 127; System.out.println(i==j);//true Integer i1 = 128; Integer j1 = 128; System.out.println(i1==j1);//false Integer i2 = new Integer(127); Integer j2= new Integer(127); System.out.println(i2==j2);//false System.out.println(200 == i); //false
相关文章推荐
- linux命令:df
- WKWebView从入门到趟坑
- MySQL mysqldump 导入/导出 结构&数据&存储过程&函数&事件&触发器
- 32个最热CPLD-FPGA论坛
- Java中BitSet使用(转)
- 有图有真相——最详细的Git说明-1
- IOS-SDWebImage 底层实现原理以及面试题相关问题的学习链接
- Service 的启动与运行
- centos 上yum命令删除补救方法
- 环境变量备忘
- JAVA使用POI获取Excel的列数与行数
- Spring写第一个应用程序
- 【数据结构】-(三)
- kvm解决1000M网卡问题
- ZCMU—1777
- Request —— 让 Node.js http请求变得超简单
- OpenCV入门四:用OpenCV读取一个图像,并使用Canny算子进行边缘提取
- 平滑滤波
- sqlldr 连接远程数据配置
- ofbiz 电商项目 apache顶级