Java中两个Long对象如何比较是否相等

做项目时,发现直接用 == 判断两个Long对象时,命名值相等, 却认为false。

搜索资料发现:如果Long的值在[-127,128]之间,用“==”判断是否相等是没问题的,如果不在这个区间,是不能用“==”的。

Long a = 4l;
Long b = 4l;
a == b //true
Long a = 128l;
Long b = 128l;
a == b //false

原因如下源码解释:

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


如果不在[-127,128]之间,则会new一个新对象,自然“==”两个不同的对象,其结果必然是false了。

解决方案:

1、使用Long中的longValue()进行转换

Long a = 128l;
Long b = 128l;
a.longValue() == b.longValue() //true


2、使用Long中的equals()

Long a = 128l;
Long b = 128l;
a.equals(b);//true

下面是该方法源码:

public boolean equals(Object obj) {
        if (obj instanceof Long) {
            return value == ((Long)obj).longValue();
        }
        return false;
    }

参考:https://blog.csdn.net/zjl_pcw/article/details/79969183

猜你喜欢

转载自blog.csdn.net/weixin_41770169/article/details/87867109