前几天在项目上看到代码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()方法后再对比
可见还是要养成多看看源码的好习惯!