java Integer初始化缓存设计

文章解释了Java中的Integer对象在值为-128到127时会使用缓存,确保相同值的对象是相等的。Integer的valueOf方法会利用这个缓存来提高性能,而超出缓存范围的Integer对象则每次都会创建新实例。此外,还提到了可以通过-XX:AutoBoxCacheMax选项自定义缓存的最大值。
摘要由CSDN通过智能技术生成

1、大家都知道在不使用new生产一个integer对象的时候在取值为 -128 到127之间生产的对象都是相等的。

 

Integer i=12;

Integer j=12;

i==j 返回true。

2、解析一下代码,看一下JDK中是怎么实现的,首先在Integer中有这么一个内部类:

 /**
     * 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.  The size of the cache
     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.  意思这个最大值是可以在jvm中设置
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    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;//默认最大是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) -1);//如果这个值大于 integer所能表示的最大值。那么我们只取 integer表示的最大值。
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);//生成 cache数组 ,所我我们这些值都在第一次调用这个内部类的时候生产并且保存到这个数组中。
        }

        private IntegerCache() {}
    }


Integer的取值方法;

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;//这个可以忽略,如果想理解可以查看 java 关键字 assert的用法。
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];//如果我们需要的integer这个值在这个缓存区间直接从数组中取出。
        return new Integer(i);
    }

3、首先我们理解一下在  Integer i=12; 中发生了什么。使用java自带的javap工具可以查看

程序如下:

public class IntegerTest{

	public  IntegerTest(){

	}	
    public static void main(String args[]) {
        Integer i=12;
        Integer j=12;
        System.out.println(i==j);
    }
} 


javap -c IntegerTest

程序在生产一个integer的时候调用了方法valueof();

4、我们可以对AutoBoxCacheMax进行设置一下,如下:

运行结果如下:

通过代码验证Integer 初始化的过程明白其中的原理。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

成风破浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值