Integer中valueOf与parseInt区别及其缓存策略

本文来聊一下Integer中的两个常用的静态方法valueOfparseInt,顺便引出基本类型包装类的缓存策略

前言

这篇文章的想法来源于一次代码检查,使用findbugs插件检查代码,然后报了如下信息(不属于bug,但是是一个更好的建议)

A boxed primitive is created from a String, just to extract the unboxed primitive value. It is more efficient to just call the static parseXXX method.

大致意思就是说把String类型转换成了包装类,而参数需要的是基本类型值,所以用parseXXX方法更有效。说实话这些方法很常用,也比较简单,就没有去看过它们到底有什么区别,直到这次偶然的机会。

源码分析

先来看下面这两个方法

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);
}

可以看到将字符串类型转换为Integer对象实则就是先去调用了parseInt(String s, int radix)方法,转换为int基本类型后,再包装为Integer对象。

看到这里其实就已经解释了上述findbugs的问题,但是可以发现,valueOf方法中先进行了范围判断,通过 IntegerCache 这个类名我们可以知道,这里做了缓存。

接下来先做个无奖竞猜,猜猜下面两行分别输出什么

System.out.println(Integer.valueOf(100) == Integer.valueOf(100));
System.out.println(Integer.valueOf(200) == Integer.valueOf(200));

相信即使不熟悉的同学看到这里也必然知道有坑哈哈,结果自己验证下就晓得了
那么就来看下 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,新建一个数组,通过for循环缓存范围内的Integer对象。
缓存最大值是可以通过设置

-Djava.lang.Integer.IntegerCache.high=xxx

来进行修改,该缓存会在首次使用Integer时初始化。
说到底这里做缓存是为了节省内存,提高性能,默认-128~127的范围是因为该范围内的数字使用频率较高。

那么既然Integer类有缓存,其他基本类型的包装类是否也做了缓存呢?
去看一下其他包装类的代码,答案是肯定的
Byte Short Long Character 都做了缓存,前三个范围固定都是-128~127
Character 缓存范围为0~127
并且只有IntegerCache的缓存范围可配置

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值