记 Integer 的一个小坑

最近用 Java 刷算法题的时候发现 Integer 有一个小坑,我把当时的代码简化如下:

public class Learn {
    public static void main(String[] args) {
        System.out.println(Integer.valueOf("66") == Integer.valueOf("66"));
        System.out.println(Integer.valueOf("222") == Integer.valueOf("222"));
        System.out.println(Integer.parseInt("222") == Integer.valueOf("222"));
    }
}

我电脑 JDK 的版本是1.8的,大家猜猜在我电脑上的输出结果是啥?


true
false
true

可能有个朋友遇到过这个问题,但我是没有的哈,当时我人傻掉了…………

本着学习和解决 Bug 的心态,我点开了 Integer 类的源码发现了问题所在:

Integer.valueOf()

我首先点开了 Integer.valueOf(String s) 方法一探究竟,然后发现:

public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

Integer.valueOf(int i):

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

parseInt():

public static int parseInt(String s, int radix)
                throws NumberFormatException
    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */

        if (s == null) {
            throw new NumberFormatException("null");
        }

        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }

        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }

        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
    }

从代码上我们可以知道两件事:

  1. Integer.valueOf(String s) 方法将字符串 s 交给 parseInt 方法处理,将其转换为十进制整数后,传给 Integer.valueOf(int i) 方法处理该字符串代表的整数

  2. 当传入数字处于[IntegerCache.low, IntegerCache.high]这个区间时,将直接返回一个数值。

那我们来看看[IntegerCache.low, IntegerCache.high]区间长度是多少吧:

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() {}
    }

分析上面的代码,IntegerCache 是一个私有的静态内部类,它作为 Integer 类的静态缓存类被 Integer 使用,当我们需要使用的整数数值处于[-128, 127]之间时,不需要重新生成一个 Integer 对象,因为已经有了相对应的静态对象,直接使用就可以了。

换言之,当我们使用[-128,127]区间内的对象,取得的始终是一个静态对象的引用,因此,我们第一个对比的输出是 true。

而第二个是 false 的原因也就好理解了,当整数不处于[-128,127]区间内时,Integer.valueOf 方法返回的是一个新建的 Integer 对象,而我们比较的时候又是用的 == 符号,因此对比的是两个数值一样,但引用不一样的 Integer 对象。

可以用以下代码验证这个想法:

        Integer i1 = Integer.valueOf("66");
        Integer i2 = Integer.valueOf("66");
        Integer i3 = Integer.valueOf("222");
        Integer i4 = Integer.valueOf("222");

Integer.parseInt()

那为什么第三种方法返回的结果又是对的呢?

这是因为第三种方法左端的 Integer.parseInt() 方法的返回值是 int 类型而不是 Integer,这就使得 == 号在比较两者时,对右边的 Integer 对象执行了自动拆箱,因此得到的结果是我们想要的。

那第一种方法能不能直接得到想要的结果呢?能!我们只需要调用 equals 方法比较两个对象就可以得到结果了。

System.out.println(Integer.valueOf("222").equals(Integer.valueOf("222")));

结果为


true

额外提醒

后来我查了一些资料,这个情况在 Java 1.7 以上的版本才会这样,因为 Integer 源码发生了修改。但有人会说,那我不用 Java 1.7 以上的版本不就好了?我觉得这样的想法是不对的,一方面,直接用 == 号比较两个对象本身就是不应该被提倡的,对象的比较应该使用的是 equals 方法;另一方面,技术总是向前走的,如果因为新技术有坑就不去用的话,人类为什么还要发明飞机和汽车呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值