为什么不要使用==比较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 ...
随机推荐
- 时间格式在ios和安卓兼容性的问题:
在做项目时,在时间显示上遇到一个问题,取后台返回的时间时,在ios手机上时间不显示,安卓手机正常,最后通过Vconsole工具发现,ios不能支持用“—”分割的时间,此外后台返回的时间格式为x年x月x ...
- 指定细则 Small
根据HTML5,small表示细则一类的旁注(side comment). “通常包括免责声明.注意事项.法律限制.版权信息等.有时我们还可以用它来表示署名,或者满足许可要求.” small通常是行内 ...
- Vue – 基础学习(2):组件间 通信及参数传递
Vue – 基础学习(2):组件间 通信及参数传递
- Redis特点分析及性能优化
一.Key >Redis key值是二进制安全的,这意味着可以可以使用任何二进制序列作为key值.空字符串也是有效的key值. >key取值原则 1.键值不需要太长,消耗内存,且在数据中查 ...
- MAC安装配置maven环境变量
1.下载maven包: 下载链接:
- centos源码编译安装新版本内核
在工作中,很多时候由于需要使用新的技术方案,需要较新版本的内核来支持新的功能,而centos系统自带的内核版本普遍都比较旧,因此我们需要对系统的内核进行升级,安装新版的内核.本文以centos7系 ...
- Win10如何设置插入鼠标后自动禁用触摸板
首先按“Windows+R”键,调出运行窗口. 然后输入“control”然后点击“确定”! 打开Win10控制面板 在“控制面板”中,选择“硬件和声音”! 选择“AUSU Amart Ges ...
- (二)Kubernetes kubeadm部署k8s集群
kubeadm介绍 kubeadm是Kubernetes项目自带的及集群构建工具,负责执行构建一个最小化的可用集群以及将其启动等的必要基本步骤,kubeadm是Kubernetes集群全生命周期的管理 ...
- centos 6.5 忘记用户名和密码
1,启动虚拟机,出现下面的倒计时界面时,按键盘上的e键 (说明:要确保光标此时已经在虚拟机内了,要不然,按了e键,也是在windows内,无效.e代表edit,启动前编辑) 2,进入如下后界面,再按e ...
- 图论篇3——最短路径 Dijkstra算法、Floyd算法
最短路径 问题背景:地图上有很多个城市,已知各城市之间距离(或者是所需时间,后面都用距离了),一般问题无外乎就是以下几个: 从某城市到其余所有城市的最短距离[单源最短路径] 所有城市之间相互的最短距离 ...