本文包含以下內容:
- [x] int 和 Integer 哪個會占用更多的內存? int 和 Integer 有什么區別?相等嗎?
int和Integer所占內存比較:
Integer對象會占用更多的內存。Integer是一個對象,需要存儲對象的元數據。但是int是一個原始類型的數據,所以占用的空間更少。
int和Integer的更多區別:
除了之前寫的通用的基本數據類型與包裝類的區別之外,還有以下方面的比較:
(注:對于引用類型變量,==操作符比較的是兩個引用是否指向同一個對象;對于基本類型變量,==操作符比較的是兩個變量的值是否相等。)
1.兩個通過new出來的Integer變量比較,結果為false。
/**
* 比較兩個new出來的Integer
*/
public class Test {
public static void main(String[] args) {
Integer i = new Integer(200);
Integer j = new Integer(200);
System.out.print(i == j);
//輸出:false
}
}
Integer變量實際上是對一個Integer對象的引用。當new一個Integer時,實際上是生成一個指針指向此對象,兩次new Integer生成的是兩個對象,其內存地址不同,所以兩個new出來的Integer變量不等。
2.非new生成的Integer變量與new Integer()生成的變量比較,結果為false。
/**
* 比較非new生成的Integer變量與new生成的Integer變量
*/
public class Test {
public static void main(String[] args) {
Integer i= new Integer(200);
Integer j = 200;
System.out.print(i == j);
//輸出:false
}
}
因為非new生成的Integer變量指向的是java常量池中的對象,而new Integer()生成的變量指向堆中新建的對象,兩者在內存中的地址不同。所以 輸出為false。
3.兩個非new生成的Integer對象進行比較,如果兩個變量的值在區間[-128,127]之間,比較結果為true;否則,結果為false。
/**
* 比較兩個非new生成的Integer變量
*/
public class Test {
public static void main(String[] args) {
Integer i1 = 127;
Integer ji = 127;
System.out.println(i1 == ji);//輸出:true
Integer i2 = 128;
Integer j2 = 128;
System.out.println(i2 == j2);//輸出:false
}
}
java在編譯Integer i1 = 127時,會翻譯成Integer i1 = Integer.valueOf(127)。查看Interger源碼:
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*/
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() {}
}
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
可以看出,java會將[-128,127]之間的數進行緩存。Integer i1 = 127時,會將127緩存,Integer j2 = 127時,就直接從緩存中取,不會new了,所以結果為true。
Integer i2 = 128時,不會將128緩存,Integer j2 = 128時,會return new Integer(128)。所以結果為false。
4. Integer變量(無論是否是new生成的)與int變量比較,只要兩個變量的值是相等的,結果都為true。
/**
* 比較Integer變量與int變量
*/
public class Test {
public static void main(String[] args) {
Integer i1 = 200;
Integer i2 = new Integer(200);
int j = 200;
System.out.println(i1 == j);//輸出:true
System.out.println(i2 == j);//輸出:true
}
}
包裝類Integer變量在與基本數據類型int變量比較時,Integer會自動拆包裝為int,然后進行比較,實際上就是兩個int變量進行比較,值相等,所以為true。