比较Integer的时候,不要用==。

查看Integer的源码,如下:

    /**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

通过注释可以知道,为了更好的空间和时间性能,Integer会缓存频繁使用的数值,数值范围为-128到127,在此范围内直接返回缓存值。

IntegerCache.low 是-128,IntegerCache.high是127,如果在这个区间内,他就会把变量i当做一个变量,放到内存中;

但如果不在这个范围内,就会去new一个Integer对象,

而如果两个Integer值都不在这个范围内,那么就会new了两个对象实例,两个对象用==比较肯定是false。

解决方法

比较Integer的值有两种方法,

1.一个是用equals()比较,但是注意要判空,避免空指针异常。

2.一个是用intValue()转成int比较。

示例如下:

        Integer value1=129;
Integer value2=129;
if(value1.intValue()==value2.intValue()){
// ...
}

参考资料:

https://blog.csdn.net/luohao_/article/details/86607686

为什么不要使用==比较Integer?的更多相关文章

  1. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  2. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  3. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  4. [LeetCode] Integer Replacement 整数替换

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  5. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  7. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  10. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

随机推荐

  1. 给任意view设置圆角outline.setRoundRect()方法实现

    效果图,给banner设置圆角,1没有直接修改imageView而是使用自带方法进行设置 具体方法如下 //圆角方法 @TargetApi(Build.VERSION_CODES.LOLLIPOP) ...

  2. Spark基于自定义聚合函数实现【列转行、行转列】

    一.分析 Spark提供了非常丰富的算子,可以实现大部分的逻辑处理,例如,要实现行转列,可以用hiveContext中支持的concat_ws(',', collect_set('字段'))实现.但是 ...

  3. Ajax跨域问题及解决方案 asp.net core 系列之允许跨越访问(Enable Cross-Origin Requests:CORS) c#中的Cache缓存技术 C#中的Cookie C#串口扫描枪的简单实现 c#Socket服务器与客户端的开发(2)

    Ajax跨域问题及解决方案   目录 复现Ajax跨域问题 Ajax跨域介绍 Ajax跨域解决方案 一. 在服务端添加响应头Access-Control-Allow-Origin 二. 使用JSONP ...

  4. Docker搭建Adminer(数据库图形化管理界面)

    1.下载官方库的adminer镜像: docker pull adminer 2.创建adminer容器:docker run --link mysql:mysql --name adminer -p ...

  5. pycharm动态设置字体放大缩小

    放大设置 File —> settings—> Keymap —>在搜寻框中输入:increase —> Increase Font Size(双击) —> 在弹出的对话 ...

  6. csv、json 文件读取

    1.CSV 文件存储 1.1 写入 简单示例 import csv with open('data.csv', 'a') as csvfile: writer = csv.writer(csvfile ...

  7. Linux 修改时区(PDT修改为CST)

    方法1: Linux 修改时区 本文链接:https://blog.csdn.net/Aritem/article/details/79213306 命令:tzselect 按照顺序依次输入5,9,1 ...

  8. canvas详解---矩形绘制

    首先,就上述绘制弧线的章节进行一个小小的补充: 如果我们使用了context.beginPath();紧接着后面的context.moveTo(x,y),可以改为context.lineTo(x,y) ...

  9. 通过PHP自带的$_SERVER判断 自动识别移动设备

    因为站点需要,手机端和PC端分离,所以通过PHP自带的$_SERVER判断 自动识别移动设备 代码如下: <?php $agent = $_SERVER['HTTP_USER_AGENT']; ...

  10. 题解:UVA10791 Minimum Sum LCM

    原题 题目大意 输入整数\(n(1\le n<2^{31})\) ,求至少两个正整数,是它们的最小公倍数为$ n$,且这些整数的和最小.输出最小的和. 有多组测试输入,以\(0\)结束. 题解 ...