java Integer.valueOf 和 Integer.parseInt 和 new Integer区别及注意事项

先看一下下面的结果

1.System.out.println(127127); //true , int type compare
2.System.out.println(128
128); //true , int type compare
3.System.out.println(new Integer(127) == new Integer(127)); //false, object compare
4.System.out.println(Integer.parseInt(“128”)==Integer.parseInt(“128”)); //true, int type compare
5.System.out.println(Integer.valueOf(“127”)==Integer.valueOf(“127”)); //true ,object compare, because IntegerCache return a same object
6.System.out.println(Integer.valueOf(“128”)==Integer.valueOf(“128”)); //false ,object compare, because number beyond the IntegerCache
7.System.out.println(Integer.parseInt(“128”)==Integer.valueOf(“128”)); //true , int type compare

解释

int整型常量比较时,== 是值比较,所以1,2返回true。1,2是值比较。

new Integer() 每次构造一个新的Integer对象,所以3返回false。3是对象比较。

Integer.parseInt每次构造一个int常量,所以4返回true。4是值比较。

Integer.valueOf返回一个Integer对象,默认在-128~127之间时返回缓存中的已有对象(如果存在的话),所以5返回true,6返回false。5,6是对象比较。

第7个比较特殊,是int 和 Integer之间的比较,结果是值比较,返回true。

总结

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

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

知识扩展

针对Integer.valueOf源码分析一下

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.valueOf源码后,你就会发现,默认的Integer缓存int常量池是可以配置的,配置方法是添加VM参数,加: -Djava.lang.Integer.IntegerCache.high=200

Intellij IDEA 运行配置的VM Options选项中添加参数即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值