關(guān)于Integer和int類型數(shù)據(jù)的內(nèi)存分析
底層代碼
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];//緩存數(shù)組 -128~127
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() {}
}
/**
* 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) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
package com.qfedu.c_baozhuang;
public class Demo3 {
public static void main(String[] args) {
Integer i1 = 10;//沒(méi)有new 直接從常量池中取的
Integer i2 = 10;//沒(méi)有new 直接從常量池中取的
System.out.println(i1 == i2);//true
Integer i3 = 1000;//new對(duì)象了
Integer i4 = 1000;//new對(duì)象了
System.out.println(i3 == i4);//false
//考察了Integer類加載的時(shí)候悯许,
// 它就創(chuàng)建了自己的靜態(tài)空間(常量池)瘫想,
// 立即加載了Integer類型的數(shù)組昧谊, static final Integer cache[];
// 數(shù)組內(nèi)存儲(chǔ)了256個(gè)Integer類型的對(duì)象-128 - 127玫霎,
// 如果我們用的對(duì)象范圍在這之內(nèi)褥蚯,是final修飾的挚冤,就意味著不會(huì)改變地址
//i1 沒(méi)有超過(guò)-128 ~127這個(gè)范圍的時(shí)候,不會(huì)改變地址的赞庶,就意味著內(nèi)存地址是一樣的
//使用==的時(shí)候训挡,比較內(nèi)存地址澳骤,所以相等!@奖 为肮!
// 如Ingeger i1 = 10;JVM就會(huì)直接從靜態(tài)區(qū)的Integer類型的緩沖數(shù)組中直接找對(duì)應(yīng)的對(duì)象肤京,
// 如果我們用的對(duì)象范圍超出了這個(gè)-128~127弥锄,例如Integer i1 = 1000;
// JVM就會(huì)幫我們?cè)诙褍?nèi)存中創(chuàng)建一個(gè)新的Integer對(duì)象蟆沫。那么內(nèi)存地址不一樣的
// 使用==比較的就是一個(gè)false
}
}
京東面試題:
Integer i1 = 10;
Intger i2 = 10;
sout(i1 == i2);//true 不會(huì)創(chuàng)建新的對(duì)象籽暇,直接從常量池
Integer i3 = 1000;
Integer i4 = 1000;
sout(i3 == i4);//false 已經(jīng)超過(guò)緩存數(shù)組的容量了-128~127 ,就要在堆區(qū)去創(chuàng)建新的對(duì)象了
總結(jié):
int類型賦值:不管值的大小,都在常量池中7古印=溆啤!
int a= 128; int a = 8999; 都在常量池中V凵健3窈!
Integer類型賦值:
Integer i1 = 10;//不會(huì)在堆區(qū)創(chuàng)建對(duì)象
Integer i2 = 1000;//堆區(qū)會(huì)創(chuàng)建對(duì)象累盗,值會(huì)指向常量池中的那個(gè)值:蟆!若债!
若值的范圍在-128~127之間符相,在常量池中!4懒铡啊终!
如果值的范圍不在-128~127之間,則在堆區(qū)創(chuàng)建對(duì)象傲须,進(jìn)行賦值了
實(shí)例化Integer對(duì)象:
Integer i1 = new Integer(12);
只要是new的Integer 使用== 都是false
9.png