Integer.valueOf和Integer.parseInt及new Integer()的区别

题目

1.System.out.println(127==127); //true , 值比较
2.System.out.println(new Integer(127) == new Integer(127)); //false, 两个Integer实例的地址不同
3.System.out.println(Integer.parseInt("128")==Integer.parseInt("128")); //true, Integer.parseInt返回的是int类型的值
4.System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); //true ,Integer.valueOf()会缓存-128 ~ 127之间的值,因此地址相同
5.System.out.println(Integer.valueOf("128")==Integer.valueOf("128")); //false ,两个对象虽然都在缓存里面,但是不是同一个对象
6.System.out.println(Integer.parseInt("128")==Integer.valueOf("128")); //true ,自动拆箱比较的是值

对于整型的比较,首先判断是值比较还是对象比较,值比较肯定返回true,有一个是值就是值比较。对象比较,则看对象是怎么构造出来的,如果是采用new Integer方式,则每次产生新对象,两个new出来的Integer比较肯定返回false,如果是Integer.valueOf方式的话,注意值的区间是否在-128~127之间,如果在,则构造的相同值的对象是同一个对象,==比较后返回true,否则返回false。

所以,对于值比较==放心没问题,对于Integer的比较最好用equals方法比较对象内容,当然注意先判断Integer是否不为null。

Integer源码分析

  1. 我们调用的Integer.valueOf方法, 它先调用parseInt转成int型数值,再调它自己的重载方法
public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
}

2.Integer.valueOf重载方法,根据数值i的大小,决定是否从缓存中取一个Integer对象

 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

3.Integer的构造函数

  public Integer(int value) {
        this.value = value;
    }

4.IntegerCache静态类,是Integer的内部类,三个属性(一个缓存的Integer型数组+一组缓存范围)

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"); //读取VM参数
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue); //配置值转换成int数值
                    i = Math.max(i, 127);  //和127比较,取较大者
                    // Maximum array size is Integer.MAX_VALUE(控制缓存数组的大小,最大为整型的最大值,这样一来,h值就必须小于整型最大值,因为要存 -128~0这129个数嘛)
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1); //实际就是 h=Math.min(i,Integer.MAX_VALUE-129),正整数能缓存的个数
                } 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++);  //将一些int常量缓存进Integer对象数组缓存中去

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127; //如果小于127,抛异常
        }

        private IntegerCache() {}
    }

默认的Integer缓存int常量池是可以配置的,配置方法是添加VM参数,加: -Djava.lang.Integer.IntegerCache.high=200

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值