在Java中Integer 和 Long 进行比较 ==问题

Integer和Long == 注意点

解析源码

在Java中基础类型包装类中的IntegerLong有一个特殊的地方需要注意
在比较值的时候不要用 == 进行比较。

在这里以 Long 类型为例
查看源码: Long.java

	public static Long valueOf(long l) {
        final int offset = 128;
        // -128 到 127 之间的数值会被缓存起来
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }
    
    private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }

再查看Integer.java,发现也有类似的缓存

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

测试案例

测试案例:

Long num1 = -129L;
        Long num2 =-129L;
        Long num3 =-128L;
        Long num4 =-128L;
        Long num5 = 127L;
        Long num6 = 127L;
        Long num7 = 128L;
        Long num8 = 128L;
        System.out.println("-129L == -129L " +(num1 == num2));
        System.out.println("-128L == -128L " +(num3 == num4));
        System.out.println("127L == 127L " +(num5 == num6));
        System.out.println("128L == 128L " +(num7 == num8));

结果如下:
在这里插入图片描述

如何比较

那么我们改如何比较呢,方法有如下几种

第一种: longValue 或 intValue
第二种: compareTo 或者 compare
第三种: 使用equals

第三种方法也是使用的第一种,Long重写了equals方法
在这里插入图片描述
案例:

		Long num1 = -129L;
        Long num2 =-129L;

        System.out.println(num1.longValue() == num2.longValue());
        System.out.println(num1.equals(num2));
        //System.out.println(num1.doubleValue() == num2.doubleValue());
        System.out.println(num1.intValue() == num2.intValue());
        System.out.println(num1.compareTo(num2));// num1 < num2 返回 -1  num1 == num2 返回0 num1 > num2 返回1
        System.out.println(Long.compare(num1, num2));// num1 < num2 返回 -1  num1 == num2 返回0 num1 > num2 返回1

结果如下:
在这里插入图片描述

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值