java中整数的相等比较

如果比较两个数值相等的Integer类型的整数,你可能会发现,用“==”比较(首先你必须明确“==”比较的是地址),有的时候返回true,而有的时候,返回false。比如:
Integer i = 128;
Integer j = 128;
System.out.println(i == j);//返回false
Integer m = 127;
Integer n = 127;
System.out.println(m == n);//返回true

为什么两个相差1的数,比较的结果却不一样呢。看一下源码

public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
Integer i = 128;这种方式赋值,会调用valueOf方法。我们发现这里做了一些关于IntegerCache的操作。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) {
                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);
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

原来Integer把-128到127(可调)的整数都提前实例化了, 所以你不管创建多少个这个范围内的Integer都是同一个对象。

那么,如何比较两个Integer类型是否相等呢,你肯定会想到equals,没错,就是equals,看下equals源码:
public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
value值是int类型:

private final int value;

通过源码得知,是获取Integer的基本类型值来用==比较了。
所以,我们也可以这样,通过Integer.intValue()获取int值来直接比较。
其实,一个int类型和Integer直接“==”比较也是没有问题的:
Integer i = 128;
int j = 128;
System.out.println(i == j);//返回true
原因是Integer类型会自动拆箱变为int类型来进行比较。

综上:
如果你用两个Integer类型的整数做相等比较

1.如果Integer类型的两个数相等,如果范围在-128~127(默认),那么用“==”返回true,其余的范会false。
2.两个基本类型int进行相等比较,直接用==即可。
3.一个基本类型int和一个包装类型Integer比较,用==也可,比较时候,Integer类型做了拆箱操作。
4.Integer类型比较大小,要么调用Integer.intValue()转为基本类型用“==”比较,要么直接用equals比较。

扩展:
Long和Short类型也做了相同的处理,只不过最大值是不可调的。
参考Long的源码:

public static Long valueOf(long l) {
        final int offset = 128;
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }

参考Short的源码:
public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        if (sAsInt >= -128 && sAsInt <= 127) { // must cache
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值