前几天在项目上看到代码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
trueProcess 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()方法后再对比

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

JAVA中Long与Integer踩的坑相关推荐

  1. java中String,int,Integer,char、double类型转换

    java中String,int,Integer,char.double类型转换----https://www.cnblogs.com/kangyu222/p/5866025.html 转载于:http ...

  2. Java中的AutoBoxing (Integer对-128~127之间数值的特殊处理)

      理解Java中Integer和int的Autoboxing,有助于java新手理解java内存机制.   在Java中的,Integer和int是可以Autoboxing和boxing的,这里要注 ...

  3. Java中int和Integer的区别

    int 是基本类型,直接存数值 integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型和复杂数据类型 int 是前者>>integer 是后者(也就是一个 ...

  4. php反序列化java.long_细数java中Long与Integer比较容易犯的错误总结

    今天使用findbugs扫描项目后发现很多高危漏洞,其中非常常见的一个是比较两个Long或Integer时直接使用的==来比较. 其实这样是错误的. 因为Long与Ineger都是包装类型,是对象. ...

  5. JAVA中int 是什么意思,java中int和Integer什么区别

    java中int和Integer的区别  一看就懂 int 是基本类型,直接存数值 integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型和复杂数据类型 int 是前 ...

  6. 016 Java中 int、Integer和 new Integer() 使用==比较

    Java中 int.Integer和 new Integer() 使用==比较 int则是java的一种基本数据类型,其定义的是基本数据类型变量 :Integer是int的包装类,其定义的是引用类型变 ...

  7. Java 中 int和Integer比较

    下文笔者讲述java中int和Integer类型的比较,如下所示 int和Integer类型比较 1:Integer是int的包装类,int则是java的一种基本数据类型 2:Integer变量必须实 ...

  8. Java中ArrayList、Integer[]和int[]的相互转换

    一.Integer[]与ArrayList的互转 1. Integer[]转ArrayList (1) 方法一: 利用Arrays工具类中的asList方法 Integer[] arr = {1,2, ...

  9. Java中Int、Integer、new Integer()之间的区别

    Java中有八种基本数据类型,Int是其中之一.而Integer是Int的封装,即Integer其实是一个对象.可以通过new来创建一个Integer对象,也可以直接将Int值赋给Integer变量. ...

  10. java中两个Integer类型的值相比较的问题

    转载自: https://www.cnblogs.com/xh0102/p/5280032.html 两个Integer类型整数进行比较时,一定要先用intValue()方法将其转换为int数之后再进 ...

最新文章

  1. 极客班C++ STL(容器)第二周笔记
  2. Angular async pipe在Component html模板中的一个实际应用
  3. crc16的c语言函数 计算ccitt_C语言为何如此重要
  4. 数据结构与算---重点复习知识
  5. thinkphp v5.0.11漏洞_ThinkPHP5丨远程代码执行漏洞动态分析
  6. Java笔记-对tomcat进一步认识
  7. IntelliJ IDEA下project概念和module的概念解释
  8. 12家存在低俗内容的网络文学企业被约谈:严肃查处 深入整改
  9. 由于 web 服务器上此资源的访问控制列表(acl)配置或加密设置,您无权查看此目录或页面。...
  10. [转发]在Visual Studio 2010/2012/2013/2015上使用C#开发Android/IOS安装包和操作步骤
  11. keras学习率下降策略
  12. acs880变频器静态辨识_ACS880变频器PID控制参数设置 -
  13. 02)高淇java300集学习笔记
  14. 浙大计算机系2001届,我的大学十年(林锐博士).doc
  15. solidworks批量图号分离_SolidWorks2014基于宏实现快速图号名称分离
  16. IOS破解软件,比较全的网站。
  17. 使用线性神经网络实现逻辑与和逻辑异或(一)
  18. AI语音机器人拿来做什么用?
  19. 单片机中晶振的工作原理
  20. vim .bashrc进入vim编辑界面之后如何退出

热门文章

  1. 高手过招 放“码”出击 | 2022 Google 全球编程比赛集结倒计时!
  2. 项目实战系列——使用python实现AHP层次分析法
  3. PS魔棒工具的使用方法
  4. 留学生的英文期刊论文怎么写?
  5. FZU 2122 又见LKity
  6. 计算机网络(2)——电路交换 报文交换 分组交换
  7. 深入浅出的讲解傅里叶变换(真正的通俗易懂)
  8. HTML网页设计:导航栏
  9. 导航栏的HTML的布局方式
  10. 宿主机上docker0 Linux 网桥设备是怎么来的?