Integer IntegerCache源码

简介: 先看一段测试结果:/*public static void main(String[] args) { Integer a = 128, b = 128; Integer c = 127, d = 127; System.

先看一段测试结果:

/*public static void main(String[] args) {
        Integer a = 128, b = 128;
        Integer c = 127, d = 127;

        System.out.println(a == b);//false
        System.out.println(c == d);//true

    }*/
    
    
    
    /*public static void main(String[] args) {
        Integer int1 = Integer.valueOf("100");
        Integer int2 = Integer.valueOf("100");
        System.out.println(int1 == int2);//true
    }*/
    
    
    public static void main(String[] args) {
        Integer int1 = Integer.valueOf("300");
        Integer int2 = Integer.valueOf("300");
        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进行优化。

 

相关文章
|
8月前
|
设计模式 缓存 Java
就因为把int改成Integer,第2天被辞了
一个程序员就因为改了生产环境上的一个方法参数,把int型改成了Integer类型,因为涉及到钱,结果上线之后公司损失惨重,程序员被辞退了。信不信继续往下看。先来看一段代码:
44 0
|
10月前
|
缓存 Java API
|
10月前
|
缓存 安全 Java
ArrayList源码分析之add 方法
ArrayList源码分析之add 方法
97 0
|
安全
Synchroinzed对Integer的问题
Synchroinzed对Integer的问题
Synchroinzed对Integer的问题
Zp
|
缓存 Java
Integer的比较和注意点
Integer的比较和注意点
Zp
69 0
Integer的比较和注意点
|
存储 缓存 Java
String源码分析
首先,将一个类分为几个部分,分别是类定义(继承,实现接口等),全局变量,方法,内部类等等,再分别对这几个部分进行说明,这样到最后类的全貌也就比较直观了
147 0
Java基础String,int,Integer类型的互相转换
Java基础String,int,Integer类型的互相转换
Java基础String,int,Integer类型的互相转换
|
存储 缓存 Java
【每天一道面试题】Integer、 new Integer() 、int的==比较,你全都了解吗
【每天一道面试题】Integer、 new Integer() 、int的==比较,你全都了解吗
117 0
【每天一道面试题】Integer、 new Integer() 、int的==比较,你全都了解吗
|
存储 Java
int 和 Integer 有什么区别
引用类型和原始类型的行为完全不同,并且它们具有不同的语义。
51 0
|
存储 缓存 Java
Integer源码阅读
Integer源码阅读