为什么不要使用==比较Integer?
比较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?的更多相关文章
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [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 ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
随机推荐
- C++ GDI图形设备接口
一.概念 1. GDI:(Graphics Device Interfase)图形设备接口,是一个应用程序与输出设备之间的中介. 一方面,GDI向应用程序提供一个与设备无关的编程环境,另一方面,它又以 ...
- box-shadow 模糊半径与扩展半径
关于box-shadow的基本用法参阅CSS3 box-shadow一章节. 此属性用来设置元素的阴影效果,语法结构如下: box-shadow:h-shadow v-shadow blur spre ...
- 英语orientaljasper鸡血石orientaljasper单词
鸡血石(orientaljasper),是辰砂条带的地开石,因鲜红色似鸡血的辰砂(朱砂)而得名.鸡血石含有辰砂(朱砂).石英.玉髓35%-45%.磁铁矿.赤铁矿6%-12%.辰砂约5%-8%. 鸡血石 ...
- Qt--多线程间的互斥
一.多线程间的互斥 临界资源--每次只允许一个线程进行访问的资源 线程间的互斥--多个线程在同一个时刻需要访问临界资源 QMute类是一把线程锁,保证线程间的互斥--利用线程锁能够保证临界资源的安全性 ...
- maven引入oracle驱动
下载驱动: https://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html 例如我这边是1 ...
- 连接mysql报"ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server"
最近在服务器上部署好的应用突然间连接不上mysql数据库,报错“ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this M ...
- Prometheus学习笔记(6)Alertmanager告警
目录 一.Alertmanager简介 二.Alertmanager部署 三.Alertmanager配置 四.自定义告警规则和发送 五.自定义告警模板 一.Alertmanager简介 Promet ...
- 【转】STM32生成的文件大小探索
一般在stm32工程使用keil编译之后,keil的build output栏目下面会出现如图所示的输出信息,其中会显示code 大小 RO-data.RW-data .ZI-data的大小.一般别人 ...
- GeoGebra 代数输入的提示和技巧
GeoGebra是Mac平台上一款自由且跨平台的动态数学软件,提供各级教育使用,包含了几何.代数.表格.图形.统计和微积分,集中在一个容易使用的软件. 确认表达式 始终按 Enter 键确认您 ...
- cetos报错“Lack of free swap space” 解决方法
前提:今天发现某台主机一直报警:swap内存被用光 1.查看了服务器的内存情况[发现在物理内存还剩余的情况下,swap开始被使用,甚至到被用光][root@note1 ~]# free -htotal ...