請判斷如下題目的輸出結果
public class IntegerTest {
public static void main(String[] args) {
Integer a=100, b = 100, c=500, d=500;
System.out.println(a == b);
System.out.println(c == d);
}
}
你可能會認爲要麼全爲true,要麼全爲false;也可能你確實對這些東西瞭解過了,認爲第一個爲true,第二個爲false。但是你清楚爲什麼嗎?其實全不對,這個題目的結果是不確定的。
1.首先我們先來了解一下Integer與int的關係
1.1java中四類八種基本數據類型,以及封裝類型
數值型:byteshort int long
字符型:char
浮點型:floatdouble
布爾型:boolean
基本數據類型 | byte(字節型) | short(短整型)) | int(整型) | long(長整型) | float(浮點型) | double(雙精度) | char(字符型) | boolean(布爾型) |
佔用字節 | 1 | 2 | 4 | 8 | 4 | 8 | 2 | 系統沒有提供size方法 |
默認值 | 0 | 0 | 0 | 0.0l | 0.0f | 0.0 | /u0000(空格) | false |
封裝器類 | Byte | Short | Integer | Long | Float | Double | Character | Boolean |
1.2 區分基本類型與封裝類型的好處
網上有很多說明,這裏就不再贅述了
2.數值比較爲什麼不確定?
2.1 賦值f1 = 100時,java內部發生了什麼
/**
* 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) {
assert IntegerCache.high >= 127;// vm配置最大值不能小於127
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
assert IntegerCache.high >= 127;// vm配置最大值不能小於127
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the -XX:AutoBoxCacheMax=<size> option. 通過該參數調整最大值high
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
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) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low));
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
-XX:AutoBoxCacheMax=<size> option. 通過該參數調整最大值high
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
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) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low));
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
從上面可以瞭解到,樹智的比較與IntegerCache的low與high相關,而low爲默認值-128,high默認爲127;所以有一個說法當integer 取值範圍-128至127之間時由於是通過 cache[]中獲取所以比較值應該爲true;
但是文章內部也會看到 最大值其實是可以變化的,通過如下信息獲得,也就是與vm配置相關,但同時這個配置值不能小於127
<span style="color:#ff0000">-XX:AutoBoxCacheMax=<size></span>
java.lang.Integer.IntegerCache.high
3.總結
Integer -128至127之間的數值比較結果:true
小於-128之間的樹智比較結果:false
大於127之間的樹智比較結果:不確定,與虛擬機配置參數有關
4.趣味思考
思考一個問題: Integer a = 200; int b = 200; a==b?
結果:相等,考慮下爲什麼?