当前位置:网站首页>Integer and = = compare
Integer and = = compare
2022-07-23 18:43:00 【thoughtCodes】
Project scenario :
Tips : Here is a brief introduction to the background of the project :
a bittler loser, No time-consuming on the problem
By the way, there is another problem here besides the seemingly one, that is integer The way of comparison :
Problem description
Tips : The problems encountered in the project are described here :
My way is : This way is : It turned out to be wrong :
Integer a1 = new Integer(1);
System.out.println(a1.toString().equals("1")); // String comparison
System.out.println(a1.equals("1")); //inteer Comparison :
Cause analysis :
Tips : Fill in the analysis of the problem here :
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
You can see whether the object of comparison is Integer Type of , If yes, turn to int The basic type is reused == Compare , The comparison is the value . So something special .
The essence of the source code is that it cannot be converted into int Type, otherwise it returns false. Of .
however
Solution :
Tips : Fill in the specific solution to the problem here :
Check the information to know Integer There is a cache array , namely -128<=value<=127, Get from the cache array Integer object . For ,q and w Both exceed the cached value , So it's all new New object of , therefore It doesn't match . So it's not difficult for us to understand ,Integer Of “” The method is to compare addresses . and Integer Of equals Methods will not have these problems , It's a direct literal comparison .
Therefore use Integer When making a comparison , Whether the distinction of exceeds the area of the cache array .
Large numbers are used for comparison integer, Small data comparison ==.
边栏推荐
- Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
- 【攻防世界WEB】难度四星12分进阶题:Cat
- Paddlenlp之UIE分类模型【以情感倾向分析新闻分类为例】含智能标注方案)
- Flame Graphs 火焰图安装与使用
- BOM系列之BOM介绍
- Does anyone get a job by self-study modeling? Don't let these thoughts hurt you
- Is 3D modeling promising? Is employment guaranteed with high salary or is it more profitable to take orders in sideline industry
- 使用kail破解wifi密码
- Where should we start to learn modeling from zero foundation? How to learn game modeling well?
- 次世代行业现状如何?90%转行建模师都在学习这套流程
猜你喜欢

【2020】【论文笔记】相变材料与超表面——

知乎二面:请问Redis 如何实现库存扣减操作和防止被超卖?

Redis【超强超细 入门教程】

SQLZOO——BBC QUIZ

ResponseBodyAdvice接口使用导致的报错及解决

Rhcsa note 4

Modeling at the beginning of learning is very confused, how to learn next generation role modeling?

入行3D建模有前景吗?就业高薪有保障还是副业接单赚钱多

Interviewer: what do you think is your biggest weakness?

类的基础
随机推荐
【2020】【论文笔记】基于二维光子晶体的光控分光比可调Y——
Block encryption mode ECB, CBC, PCBC, CFB, OFB, CTR
UAV circulating an unknown target under a GPS deniedenvironment with range only measurements translation
关于分组查询的一道sql题
日志框架【详解学习】
银行业如何实现数字化转型风口全速启航
How to capture the analyst rating data of Sina Financial Data Center?
MySQL transactions, starting with redo log, bin log, undo log
手写bind、call、apply其实很简单
Paddlenlp之UIE分类模型【以情感倾向分析新闻分类为例】含智能标注方案)
【攻防世界WEB】难度四星12分进阶题:Cat
Stack / heap / queue question brushing (medium)
MySQL performance tuning
integer 和==比较
Flutter operation mode
1259. 不相交的握手 動態規劃
serialization and deserialization
【2020】【论文笔记】基于Rydberg原子的——
Modeling just learning is very confused. How to learn the next generation role modeling process?
BOM introduction of BOM series