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();java valueof
}
return false;
}
由代码可以看出,⽤equal判断,Long会⾃动调⽤longValue()⽅法后再对⽐
可见还是要养成多看看源码的好习惯!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论