Integer 中的缓存类IntegerCache
2017-04-21 11:39
141 查看
题目:public class test {
public static void main(String[] args) {
Integer c = 127, d = 127;
Integer a = 128 , b = 128;
Integer int1 = Integer.valueOf("100");
Integer int2 = Integer.valueOf("100");
System.out.println(int1 == int2);//true
System.out.println(a == b);//false
System.out.println(c == d);//true
}
}结果是:
原来Integer把-128到127(可调)的整数都提前实例化了。 这就解释了那道面试题的答案,原来你不管创建多少个这个范围内的Integer用ValueOf出来的都是同一个对象。
但是为什么JDK要这么多此一举呢? 我们仔细想想, 淘宝的商品大多数都是100以内的价格, 一天后台服务器会new多少个这个的Integer, 用了IntegerCache,就减少了new的时间也就提升了效率。同时JDK还提供cache中high值得可配置,
这无疑提高了灵活性,方便对JVM进行优化。
参考Long的源码:
public static void main(String[] args) {
Integer c = 127, d = 127;
Integer a = 128 , b = 128;
Integer int1 = Integer.valueOf("100");
Integer int2 = Integer.valueOf("100");
System.out.println(int1 == int2);//true
System.out.println(a == b);//false
System.out.println(c == d);//true
}
}结果是:
true false true可是为什么呢?翻阅DK的源码,发现:
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
- Integer 中的缓存类IntegerCache
- HttpRuntime.Cache .Net自带的缓存类
- Java之IntegerCache简单介绍
- Java 中Integer的cache
- Integer IntegerCache源码
- 初学者对自动装箱和自动拆箱的认识(IntegerCache的缓存数组)
- 关于Integer.IntegerCache
- 谈谈Integer中的静态类IntegerCache
- .net System.Web.Caching.Cache 缓存类使用详解
- 有趣的IntegerCache
- HttpRuntime.Cache .Net自带的缓存类
- Java JDK Integer Cache
- Integer IntegerCache源码阅读
- Java Integer Cache
- PHP原生:分享一个轻量级的缓存类=>cache.php
- java.lang.Integer$IntegerCache
- Integer源码浅析---IntegerCache和ValueOf
- JDK5.0自动装箱拆箱注意IntegerCache的范围
- 黑马程序员-Integer类中的内部类IntegerCache