为什么不要使用==比较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 ...
随机推荐
- Serverless
一.介绍 是指依赖于第三方应用程序或服务来管理服务器端逻辑的应用程序. 此类应用程序是基于云的数据库(如Google Firebase)或身份验证服务. 无服务器也意味着开发为事件触发的代码,并且在无 ...
- Computer Networking: A Top Down Approach
目录 Chapter 1: Computer Networks and the Internet 1. What is the Internet? 2. The Network Edge 3. The ...
- [Flutter] 实现Flutter App内更新
app内实现根据安卓和IOS平台进行更新 时间匆忙,相关操作以及信息都写在代码注释里面了,闲时在补充和完善. 功能在android项目中测试可用,ios上还未进行测试,如果ios有问题或者没问题的话都 ...
- SAP技术 - How to create a CDS redirect view for a given database table
Scenario Suppose we have a database table A, and then we create a CDS redirect view B for it, then e ...
- Python3 import tensorflow 出现FutureWarning: Passing (type, 1) or '1type' 问题
解决python调用TensorFlow时出现FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate ...
- poj1502 MPI Maelstrom(单源最短路)
题意:表面乍一看output是输出最小值,但仔细研究可以发现,这个最小值是从点1到所有点所花时间的最小值,其实是访问这些节点中的最大值,因为只有访问了最长时间的那个点才算访问了所有点.所以求最短路之后 ...
- 【比赛游记】(THUPC,CTS,APIO)2019四连爆蛋记
5 月 11 日 坐飞机来到帝都,报道 THUPC. 试机题有皮配,不会. 晚上吃全聚德,喝星巴克.奢侈. 5 月 12 日 早上打 THUPC. 咕到 9 点半开始,到 2 点半结束.
- git 撤销merge
如果没有别的操作,直接回到上一次提交就可以了,在a分支执行 git reset --hard HEAD~ 会回到未merge前的状态,清空暂存区,销毁数据,如果没有推送到远程,数据就会被覆盖无法恢复, ...
- iis站点设置错误页面返回http状态码为404而不是302或其他
今天一位客户说网站错误页面返回的状态码是302而不是404,问ytkah要如何处理.这个应该是设置没有正确的原因.我们一步步来排查一下.1.首先打开iis管理器,左侧选择具体的站点,在右侧窗口中点击4 ...
- The difference between Virtual DOM and DOM
dom是结构化的文本信息的抽象,是结构化的文本信息在内存中的表示 是操作结构化文本信息的api. Follow: Follow React attacks us with the virtual DO ...