Integer IntegerCache源碼閱讀
先看一段測(cè)試結(jié)果:
/*public static void main(String[] args) {
Integer a = 128, b = 128;
Integer c = 127, d = 127;
System.out.println(a == b);//false
System.out.println(c == d);//true
}*/
/*public static void main(String[] args) {
Integer int1 = Integer.valueOf("100");
Integer int2 = Integer.valueOf("100");
System.out.println(int1 == int2);//true
}*/
public static void main(String[] args) {
Integer int1 = Integer.valueOf("300");
Integer int2 = Integer.valueOf("300");
System.out.println(int1 == int2);//false
}
JDK的源碼如下:
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
發(fā)現(xiàn)里面另有玄機(jī),多了個(gè)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() {}
}
原來(lái)Integer把-128到127(可調(diào))的整數(shù)都提前實(shí)例化了。
這就解釋了答案,原來(lái)你不管創(chuàng)建多少個(gè)這個(gè)范圍內(nèi)的Integer用ValueOf出來(lái)的都是同一個(gè)對(duì)象。
但是為什么JDK要這么多此一舉呢? 我們仔細(xì)想想, 淘寶的商品大多數(shù)都是100以內(nèi)的價(jià)格, 一天后臺(tái)服務(wù)器會(huì)new多少個(gè)這個(gè)的Integer, 用了IntegerCache,就減少了new的時(shí)間也就提升了效率。同時(shí)JDK還提供cache中high值得可配置,
這無(wú)疑提高了靈活性,方便對(duì)JVM進(jìn)行優(yōu)化。
總結(jié)
以上就是本文關(guān)于Integer IntegerCache源碼閱讀的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
springboot實(shí)現(xiàn)配置多環(huán)境yml方式
在SpringBoot項(xiàng)目中,通過(guò)創(chuàng)建不同的YAML配置文件來(lái)實(shí)現(xiàn)多環(huán)境配置是一種常見(jiàn)且有效的方法,這些配置文件包括application.yml、application-dev.yml、application-prod.yml等,分別對(duì)應(yīng)不同的開(kāi)發(fā)環(huán)境,如開(kāi)發(fā)環(huán)境、生產(chǎn)環(huán)境2024-11-11
java代碼實(shí)現(xiàn)C盤文件統(tǒng)計(jì)工具
今天周末,給大家分享基于java代碼實(shí)現(xiàn)C盤文件統(tǒng)計(jì)工具,在這小編使用的版本是Maven-3.9.9,jdk1.8,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07
Spring boot如何配置請(qǐng)求的入?yún)⒑统鰠son數(shù)據(jù)格式
這篇文章主要介紹了spring boot如何配置請(qǐng)求的入?yún)⒑统鰠son數(shù)據(jù)格式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
在X分鐘內(nèi)學(xué)會(huì)C#的入門簡(jiǎn)明教程
C#是一個(gè)優(yōu)雅的、類型安全的面向?qū)ο笳Z(yǔ)言。使用C#,開(kāi)發(fā)者可以在.NET框架下構(gòu)建安全而強(qiáng)大的應(yīng)用程序,閱讀本文可以快速的入門C#編程語(yǔ)言,需要的朋友可以參考下2014-03-03
Java面試官最喜歡問(wèn)的關(guān)鍵字之volatile詳解
這篇文章主要給大家介紹了關(guān)于Java面試官最喜歡問(wèn)的關(guān)鍵字之volatile的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

