public class IntegerTest {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
}
}
複製代碼
以上代碼的執行結果爲:java
true false緩存
首先,當咱們將以上的測試代碼編譯爲字節碼(.class)以後,編碼的代碼以下:測試
public class IntegerTest {
public static void main(String[] paramArrayOfString) {
Integer integer1 = Integer.valueOf(127);
Integer integer2 = Integer.valueOf(127);
System.out.println((integer1 == integer2));
Integer integer3 = Integer.valueOf(128);
Integer integer4 = Integer.valueOf(128);
System.out.println((integer3 == integer4));
}
}
複製代碼
能夠看出在建立 Integer 時使用到了 valueOf
,它的實現源碼以下:this
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
複製代碼
從上述源碼中能夠看出這個方法中使用了 IntegerCache,IntegerCache 的源碼以下:編碼
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
複製代碼
從上述源碼能夠看出,在 Integer 的取值在 -128 到 127 之間時,它會複用已有的對象,所以在 i1(127)和 i2 使用 == 對比時值纔會爲 true,而當取值變爲 128 時,則執行的結果爲 false。spa
這一點其實在阿里巴巴的《Java開發手冊》中也有相應的規定,規定的內容以下:3d
【強制】全部整型包裝類對象之間值的比較,所有使用 equals 方法比較。code
說明:對於 Integer var = ? 在 -128 至 127 之間的賦值,Integer 對象是在 IntegerCache.cache 產生, 會複用已有對象,這個區間內的 Integer 值能夠直接使用 == 進行判斷,可是這個區間以外的全部數據,都 會在堆上產生,並不會複用已有對象,這是一個大坑,推薦使用 equals 方法進行判斷。orm
不只如此,當咱們使用 new Integer 時,不管值爲多少都不能使用 == 比較,示例代碼以下:cdn
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);
}
}
複製代碼
以上代碼的執行結果爲:
false
這是由於 new Integer 方法並無使用到 IntegerCache,而是直接建立了新對象,所以就不能用 == 比較了。
小貼士:== 是用來直接比對兩個對象的引用是否相同的,而 equals 則是用來對比兩個對象的值是否相同的。
由於 Integer 類實現了 Comparable 接口,所以咱們可使用 compareTo 來對比兩個值的大小,實現源碼以下:
public final class Integer extends Number implements Comparable<Integer> {
// 忽略其餘內容
}
複製代碼
compareTo 的使用以下:
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = new Integer(128);
Integer i2 = new Integer(128);
System.out.println(i1.compareTo(i2));
}
}
複製代碼
以上代碼的執行結果爲:
0
compareTo 的源碼以下:
public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
複製代碼
由此能夠看出 compareTo 的返回值總共有三個:-一、0、1,其中 -1 表示前一值小於後一個值;0 表示兩個值相等;1 表示前一個值大於後一個值,所以咱們用它來比較兩個 Integer 的值是否相等。
compareTo 方法給咱們了一個啓發,咱們能夠直接將兩個值進行相減,若是相減的值等於 0,則說明對比的兩個值是相同的,示例代碼以下:
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = new Integer(128);
Integer i2 = new Integer(128);
System.out.println((i1 - i2) == 0);
}
}
複製代碼
以上代碼的執行結果爲:
true
咱們可使用 intValue 獲得 Integer 的 int 值,而後再使用 == 進行比較,示例代碼以下:
public class IntegerTest {
public static void main(String[] args) {
Integer i = 558;
Integer i2 = 558;
System.out.println(i.intValue() == i2.intValue());
}
}
複製代碼
以上代碼的執行結果爲:
true
異或是一個數學運算符,它應用於邏輯運算。在計算機中若是 a、b 兩個值不相同,則異或結果爲 1;若是 a、b 兩個值相同,異或結果爲 0。
好比:
異或實現示例以下:
public class IntegerTest {
public static void main(String[] args) {
Integer i = 558;
Integer i2 = 558;
System.out.println((i ^ i2) == 0);
}
}
複製代碼
以上代碼的執行結果爲:
true
IntegerCache 默認的取值範圍爲 -128 到 127,但咱們能夠經過設置啓動參數來調整 IntegerCache 的最大緩存值,好比咱們能夠配置虛擬機的啓動參數 -XX:AutoBoxCacheMax=1000
,此配置表示將緩存的最大值設置爲 1000,若是是 Idea 的配置以下:
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = 999;
Integer i2 = 999;
System.out.println(i1 == i2);
}
}
複製代碼
以上代碼的執行結果爲:
true
從運行的結果能夠看出 IntegerCache 的取值範圍被成功的更改了。
本文咱們介紹了 Integer 的 6 種比較方式:==、equals、compareTo、直接運算,而 == 方式並不能用於 Integer 的比較,它只適用於非 new Integer 的必定範圍內(-128~127),然後 5 種方式均可以正經常使用於 Integer 的比較,其中 equals 的比較方式是最爲經常使用的。
除了以上幾種比較方式以外,你還知道其餘的比較方式嗎?歡迎評論區補充留言。
關注公衆號「Java中文社羣」訂閱更多精彩。