欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Java

當(dāng)前位置:主頁 > 軟件編程 > Java >

Java包裝類的緩存機(jī)制原理實(shí)例詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Java|點(diǎn)擊: 次

這篇文章主要介紹了Java包裝類的緩存機(jī)制原理實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

Integer包裝類

舉個(gè)栗子:

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

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

使用自動裝箱將基本類型轉(zhuǎn)為封裝類對象這個(gè)過程其實(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中獲取對象

看一下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之間,范圍的最大值可以通過java.lang.Integer.IntegerCache.high設(shè)置,通過for循環(huán)將范圍內(nèi)的數(shù)據(jù)實(shí)例化為Integer對象放到cache數(shù)組里

在測試一下:

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

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

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沒有使用緩存,直接new的對象

總結(jié):

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:java課程設(shè)計(jì)之坦克大戰(zhàn)

欄    目:Java

下一篇:springboot2.0使用Hikari連接池的方法(替換druid)

本文標(biāo)題:Java包裝類的緩存機(jī)制原理實(shí)例詳解

本文地址:http://mengdiqiu.com.cn/a1/Java/8900.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有