Integer与int值的比较

==一般用于比较内存地址,equals()用于比较Object的值,注意int用equals()是会报错的。
Integer i=1
Integer k=1
i.equals(k)=true
i==k=true
i.intValue()==k.intValue()=true


Integer l=129
Integer p=129
l.equals(p)=true
l==p=false
l.intValue()==p.intValue()=true

综上所属Integer在栈中的值-128到127是成立的


int o=197
int y=197
o==y=true


Integer A=197
Integer B=197
A==B=false

同样值是197,但是Integer是重新new了对象的,所以A==B=false。

而int则是将两个相同的值指向了一个地址o==y=true。


o==B=true

这是我们很多人代码没有出错的原因之一,比如从数据库查出的id用==判断而没出错。

猜你喜欢

转载自www.cnblogs.com/zeussbook/p/9296744.html