2014年去某公司笔试的时候遇到这么一道题:

public class Test {
public static void main(String[] args) {
Integer int1 = Integer.valueOf("100");
Integer int2 = Integer.valueOf("100");
System.out.println(int1 == int2);
}
}

问打印的结果的多少? 但是我回答的是false, 后来仔细想想应该没有这个简单,就翻了下JDK的源码,发现:

public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
} public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

发现里面另有玄机,多了个IntegerCache类:

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) {
try {
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);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
} private IntegerCache() {}
}

原来Integer把-128到127(可调)的整数都提前实例化了。 这就解释了那道面试题的答案,原来你不管创建多少个这个范围内的Integer用ValueOf出来的都是同一个对象。

但是为什么JDK要这么多此一举呢? 我们仔细想想, 淘宝的商品大多数都是100以内的价格, 一天后台服务器会new多少个这个的Integer, 用了IntegerCache,就减少了new的时间也就提升了效率。同时JDK还提供cache中high值得可配置,

这无疑提高了灵活性,方便对JVM进行优化。

参考Long的源码:

private static class LongCache {
private LongCache(){} static final Long cache[] = new Long[-(-128) + 127 + 1]; static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}

Long也做了缓存,只是没有提供调整机制, 在Short中类似:

private static class ShortCache {
private ShortCache(){} static final Short cache[] = new Short[-(-128) + 127 + 1]; static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}

Integer 中的缓存类IntegerCache的更多相关文章

  1. Integer 中的缓存类 IntegerCache

    我们先看一段代码: public class TestAutoBoxing { public static void main(String[] args) { //-128到127之间 Intege ...

  2. java的Integer中也会有缓存

    在上篇<java的自动拆箱会发生NPE>博客中接收了java中的Integer中的自动拆箱产生的NPE,其实对于所有的包装类来说都是一样的,都会产生这样的问题,大家需要举一反三,做学问学知 ...

  3. yii2.0中数据缓存之增删改查

    public function actionSss(){ /* * 获取到缓存 * 这里是获取的是根目录下 的common/main.php中的缓存类组件 * */ $cache=\Yii::$app ...

  4. 对Integer类中的私有IntegerCache缓存类的一点记录

    对Integer类中的私有IntegerCache缓存类的一点记录 // Integer类有内部缓存,存贮着-128 到 127. // 所以,每个使用这些数字的变量都指向同一个缓存数据 // 因此可 ...

  5. 谈谈Integer中的静态类IntegerCache

            学习的本质就是一个赋值的过程,用新知识来覆盖你的旧知识或者无知(null).掌握知识是自己的, 分享知识,才能帮助更多的人,创造更大的价值.学贵以恒,以此自勉,与君共享.----曦阳X ...

  6. Java中的BigDecimal类和int和Integer总结

    前言 我们都知道浮点型变量在进行计算的时候会出现丢失精度的问题.如下一段代码: System.out.println(0.05 + 0.01); System.out.println(1.0 - 0. ...

  7. ASP.NET中使用Cache类来缓存页面的信息

    实现 如果将数据保存在全局应用程序对象Application中,值将会在程序运行时一直存在,而我们只需要缓存一段时间. ASP.NET提供了一个Cache对象来执行对象数据的缓存. Cache对象是S ...

  8. Java自动装箱中的缓存原理

    今天看到一道'经典'面试题: Integer a = 100; Integer b = 100; System.out.println(a==b); Integer a2 = 200; Integer ...

  9. java整形中的缓存机制

      英文原文:Java Integer Cache 翻译地址:Java中整型的缓存机制 原文作者:Java Papers 翻译作者:Hollis 转载请注明出处. 本文将介绍Java中Integer的 ...

随机推荐

  1. (47) odoo详细操作手册

    odoo 8 详细操作手册, ERP(Odoo8.0)操作手册-v1.10(陈伟明).pdf 链接: http://pan.baidu.com/s/1hsp0bVQ 密码: r9tt 花了将近9个月时 ...

  2. php date("Y-m-d H:i:s") 出现警告信息

    使用: $createtime=date("Y/m/d H:i:s", time()+16*60*60); $createtime=date("Y-m-d H:i:s&q ...

  3. Vertica并发DML操作性能瓶颈的产生与优化(转)

    文章来源:中国联通网研院网优网管部IT技术研究团队 作者:陆昕 1. 引言 众所周知,MPP数据库以其分布式的超大存储能力以及列式的高速汇总能力,已经成为大数据分析比不可少的工具.Vertica就是这 ...

  4. EasyUI combobox 下拉高度自适应

    要指出的是,combobox是继承自combo的,所以,combo的属性也可以被combobox使用,该问题也是这样产生的,知道这个原理,该问题就解决一半了,另一点要指出的是,在easyui中,num ...

  5. JQmeter显示进度条

    <script type="text/javascript"> $(function(){ var percent = "${evICVDBean.resul ...

  6. mysql分表技术(学习心得)

    (最近在学习mysql优化的一些问题,以下为个人一些收获,如有不足,敬请提出!) 概述:当一个表的数据很大,比如200G,这时太大,我们只靠索引也不好使了,这时我们需要分表和分区处理.分表有两种形式( ...

  7. iOS版本更新的App提交审核流程

    App的版本更新估计是在所难免的了!更新App和新的App发布有何不同了?今天我们一起来看看吧!在发布App的时候我们需要通过开发者帐号——(申请)——>发布证书(需要钥匙串对证书签名也叫加密( ...

  8. 使用nodejs调用微信发送红包

    前置条件:申请微信发送红包的账户及其权限 依赖 blueimg-md5和 xmlreader 库 /common/weixin.js 源码 /** * Created by chent696 on 2 ...

  9. app上传到App Store的快捷方法及步骤

    跳过证书的申请及配置概要文件的设置, 现在根据已有的配置概要文件及发布证书开始: 1.先在Xcode上的PROJECT和TARGETS->Build Setting->Code Signi ...

  10. 《数据结构》2.2顺序表(sequence list)

    //顺序表节点的定义 typedef struct { datatype data[MAXSIZE]; //数组容量的上限 int len; //记录最后一个元素的位置,相当于一个指针,表空时len= ...