判斷Integer值相等最好用equals

2021-09-23 14:27:50 字數 482 閱讀 7530

integer c = 3;

integer d = 3;

integer e = 321;

integer f = 321;

system.out.println(c  d);

system.out.println(e  f);

輸出 true false

integer為物件判斷是否相等還是使用equals最靠譜,

int為基本型別,判斷是否相等就是可以使用==.

原因:static final integer cache = new integer[-(-128) + 127 + 1];

static

}這是原始碼中的,也就是說cache中已有-128到127,不在這範圍的會新new ,這時可以理解比較是記憶體位址,

也就是是不是同一物件.

所以說當integer的值不在-128到127的時候使用==方法判斷是否相等就會出錯,在這個範圍之內的就會沒有問題

判斷Integer值相等最好不用

今天在開發中判斷兩個integer值相等,integer a 3 duixiang duixiang new duixiang duixiang dao.getduixiang integer b duixiang.getb system.out.print a b system.out.print...

判斷Integer值相等最好不用 未整理

今天在開發中判斷兩個integer值相等,integer a 3 duixiang duixiang new duixiang duixiang dao.getduixiang integer b duixiang.getb system.out.print a b system.out.print...

Integer值判斷是否相等問題

今天發現了乙個奇怪的問題 integer allrightstotal 140 integer allrightslasttotal 140 if allrightstotal allrightsinit 最後得出的結論是 對於integer值比較有範圍規定 integer 型別的值在 128,12...