Java 如何判断两个Long类型是否相等

目录

1、使用场景

2、发现问题     

3、问题所在(源码分析)  

3、成果展现

4、总结

5、参考文章


1、使用场景

        最近在做一个海外的项目的时候发现,如果直接使用Long类型的直接使用==结果发现两者不相等(因为我比较两个记录的主键id bigInteger的)。于是在网上找寻一下原因,结合实际情况记录一下避免下次出现类似的问题。同时通过此文章能够帮助到其他同仁。

2、发现问题     

/**
 * 两个长整型(Long)比较相等
 */
public class LongTypeEquals {
    public static void main(String[] args) {
        Long oneNum=100L;
        Long twoNum=100L;
        Long oneCommNum127=127L;
        Long twoCommNum127=127L;

        Long oneCommNum128=128L;
        Long twoCommNum128=128L;
        Long oneBigNum=10000L;
        Long twoBigNum=10000L;
        //大于-128 小于127的数
        if(oneNum==twoNum){
            System.out.println("oneNum == twoNum");
        }else{
            System.out.println("oneNum != twoNum");
        }
        // 如果大于127的情况
        if(oneCommNum127==twoCommNum127){
            System.out.println("oneCommNum127 == twoCommNum127");
        }else{
            System.out.println("oneCommNum127 != twoCommNum127");
        }

        // 如果大于127的情况
        if(oneCommNum128==twoCommNum128){
            System.out.println("oneCommNum128 == twoCommNum128");
        }else{
            System.out.println("oneCommNum128 != twoCommNum128");
        }

    }
}

返回比较结果

3、问题所在(源码分析)  

 public static Long valueOf(long var0) {
        return var0 >= -128L && var0 <= 127L ? Long.LongCache.cache[(int)var0 + 128] : new Long(var0);
    }

从源码上分析如果Long类型的值在-128和128区间[-128,127] 内可以直接使用相等比较返回正常。如果在这个区间之外的数据就只能使用Long的对象比较equals或者Long.valueOf取值后比较

  改进后的比较方法如下所示:

/**
 * 两个长整型(Long)比较相等
 */
public class LongTypeEquals {
    public static void main(String[] args) {
        Long oneNum=100L;
        Long twoNum=100L;
        Long oneCommNum127=127L;
        Long twoCommNum127=127L;

        Long oneCommNum128=128L;
        Long twoCommNum128=128L;
        Long oneBigNum=10000L;
        Long twoBigNum=10000L;
        //大于-128 小于127的数
        if(oneNum==twoNum){
            System.out.println("oneNum == twoNum");
        }else{
            System.out.println("oneNum != twoNum");
        }
        // 如果大于127的情况
        if(oneCommNum127==twoCommNum127){
            System.out.println("oneCommNum127 == twoCommNum127");
        }else{
            System.out.println("oneCommNum127 != twoCommNum127");
        }

        // 如果大于127的情况
        if(oneCommNum128==twoCommNum128){
            System.out.println("oneCommNum128 == twoCommNum128");
        }else{
            System.out.println("oneCommNum128 != twoCommNum128");
        }

        //equals 方式比较
        if(oneBigNum.equals(twoBigNum)){
            System.out.println("oneBigNum == twoBigNum");
        }else{
            System.out.println("oneBigNum != twoBigNum");
        }

        // longValue 比较
        if(oneBigNum.longValue()==twoBigNum.longValue()){
            System.out.println("oneBigNum == twoBigNum");
        }else{
            System.out.println("oneBigNum != twoBigNum");
        }

    }
}

3、成果展现

4、总结

    包装类型 最好用.eques()方法,当然也可以用.longValue()的!

5、参考文章

        Java中判断两个Long类型是否相等

猜你喜欢

转载自blog.csdn.net/jianxia801/article/details/109155293