Java包裝類的緩存機(jī)制原理實(shí)例是什么-創(chuàng)新互聯(lián)

這篇文章給大家介紹Java包裝類的緩存機(jī)制原理實(shí)例是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)浙江,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220

java 包裝類的緩存機(jī)制,是在Java 5中引入的一個(gè)有助于節(jié)省內(nèi)存、提高性能的功能,只有在自動(dòng)裝箱時(shí)有效

Integer包裝類

舉個(gè)栗子:

Integer a = 127;Integer b = 127;System.out.println(a == b);

這段代碼輸出的結(jié)果為true

使用自動(dòng)裝箱將基本類型轉(zhuǎn)為封裝類對(duì)象這個(gè)過(guò)程其實(shí)底層實(shí)現(xiàn)是調(diào)用封裝類的valueOf方法:

Integer a =127; 相當(dāng)于 Integer a = Integer.valueOf(127);

看一下Integer的valueOf方法:

public static Integer valueOf(int i) {  if (i >= IntegerCache.low && i <= IntegerCache.high)    return IntegerCache.cache[i + (-IntegerCache.low)];  return new Integer(i);}

如果入?yún)?i 大于等于IntegerCache.low或者小于等于IntegerCache.high),就從IntegerCache中獲取對(duì)象

看一下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() {}}

默認(rèn)范圍為:-128到127之間,范圍的較大值可以通過(guò)java.lang.Integer.IntegerCache.high設(shè)置,通過(guò)for循環(huán)將范圍內(nèi)的數(shù)據(jù)實(shí)例化為Integer對(duì)象放到cache數(shù)組里

在測(cè)試一下:

Integer a = 128;Integer b = 128;System.out.println(a == b);

輸出結(jié)果為false,所以如果沒(méi)有指定cache較大值時(shí),在-128到127之間使用自動(dòng)裝箱時(shí),會(huì)使用緩存

Byte包裝類

再舉個(gè)栗子:

public static void main(String[] args) {  Byte a = 127;  Byte b = 127;  System.out.println(a == b); //true}

由于Byte范圍在-128到127之間,所以Byte的valueOf都是從ByteCache緩存中獲取的

public static Byte valueOf(byte b) {  final int offset = 128;  return ByteCache.cache[(int)b + offset];}

ByteCache類:

private static class ByteCache {  private ByteCache(){}  static final Byte cache[] = new Byte[-(-128) + 127 + 1];  static {    for(int i = 0; i < cache.length; i++)      cache[i] = new Byte((byte)(i - 128));  }}

與IntegerCache相比,ByteCache的較大值是不能修改的就是127

Short包裝類

public static Short valueOf(short s) {  final int offset = 128;  int sAsInt = s;  if (sAsInt >= -128 && sAsInt <= 127) { // must cache    return ShortCache.cache[sAsInt + offset];  }  return new Short(s);}

ShortCache類:

private static class ShortCache {  private ShortCache(){}  static final Short cache[] = new Short[-(-128) + 127 + 1];  static {    for(int i = 0; i < cache.length; i++)      cache[i] = new Short((short)(i - 128));  }}

ShortCache的較大值也不可以修改,范圍只能在-128 ~ 127之間

Long包裝類的valueOf方法和LongCache類與Short包裝類的實(shí)現(xiàn)一致,范圍也是只能在-128 ~ 127之間

Character包裝類

valueOf方法:

public static Character valueOf(char c) {  if (c <= 127) { // must cache    return CharacterCache.cache[(int)c];  }  return new Character(c);}

CharacterCache類:

private static class CharacterCache {  private CharacterCache(){}  static final Character cache[] = new Character[127 + 1];  static {    for (int i = 0; i < cache.length; i++)      cache[i] = new Character((char)i);  }}

Character的緩存范圍在0 ~ 127之間

Boolean包裝類

valueOf方法:

public static Boolean valueOf(boolean b) {  return (b ? TRUE : FALSE);}

TRUE跟FALSE都是static final修飾的靜態(tài)變量

public static final Boolean TRUE = new Boolean(true);public static final Boolean FALSE = new Boolean(false);

Float包裝類 & Double包裝類

valueOf方法:

public static Float valueOf(float f) {  return new Float(f);}public static Double valueOf(double d) {  return new Double(d);}

Float和Double沒(méi)有使用緩存,直接new的對(duì)象

總結(jié):

java的包裝類中:Byte,Short,Integer,Long,Character使用static代碼塊進(jìn)行初始化緩存,其中Integer的較大值可以通過(guò)java.lang.Integer.IntegerCache.high設(shè)置;Boolean使用static final實(shí)例化的對(duì)象;Float和Double直接new的對(duì)象沒(méi)有使用緩存

關(guān)于Java包裝類的緩存機(jī)制原理實(shí)例是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

文章標(biāo)題:Java包裝類的緩存機(jī)制原理實(shí)例是什么-創(chuàng)新互聯(lián)
URL分享:http://www.muchs.cn/article0/pcsoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)用戶體驗(yàn)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管