JAVA中Long与Integer踩的坑

 前几天在项目上看到代码2个Long型变量判断是否相等用的是==,代码在测试环境部署测试没有问题,放到预发环境就有问题,于是就在main进行了如下测试

public class TestDemo {

    public static void main(String args[]){
        Long a=50L;
        Long b=50L;
        Long c=5000l;
        Long d=5000l;
        Long e=new Long(18);
        Long f=new Long(18);
        System.out.println(a==b);
        System.out.println(c==d);
        System.out.println(e==f);
        System.out.println(e.equals(f));
    }

}
true
false
false
true


Process finished with exit code 0

从代码运行结果来看判断Long型变量不能用==,只能用equal,那么就分析下原因:

1首先我们把一个long型数值赋予给了Long对象,这个过程其实就是基本类型到包装类到封装,Java上叫做装箱,其实就是Long对象调用Long.valueOf(long) 的操作

2那么点进去Long.valueOf(long)的源码

/**
 * Returns a {@code Long} instance representing the specified
 * {@code long} value.
 * If a new {@code Long} instance is not required, this method
 * should generally be used in preference to the constructor
 * {@link #Long(long)}, as this method is likely to yield
 * significantly better space and time performance by caching
 * frequently requested values.
 *
 * Note that unlike the {@linkplain Integer#valueOf(int)
 * corresponding method} in the {@code Integer} class, this method
 * is <em>not</em> required to cache values within a particular
 * range.
 *
 * @param  l a long value.
 * @return a {@code Long} instance representing {@code l}.
 * @since  1.5
 */
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);
}

看源码上有个判断,当值大于-128并且小于127的时候从JVM缓存取出值,否则就new一个新对象,这就是我们用==判断会出问题的原因

3我们用equal为啥可以,我们看看Long对象的equals方法

/**
 * Compares this object to the specified object.  The result is
 * {@code true} if and only if the argument is not
 * {@code null} and is a {@code Long} object that
 * contains the same {@code long} value as this object.
 *
 * @param   obj   the object to compare with.
 * @return  {@code true} if the objects are the same;
 *          {@code false} otherwise.
 */
public boolean equals(Object obj) {
    if (obj instanceof Long) {
        return value == ((Long)obj).longValue();
    }
    return false;
}

由代码可以看出,用equal判断,Long会自动调用longValue()方法后再对比

可见还是要养成多看看源码的好习惯!


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值