Java包装类的缓存机制原理实例是什么
创始人
2024-12-24 16:21:57
0次

java 包装类的缓存机制,是在Java 5中引入的一个有助于节省内存、提高性能的功能,只有在自动装箱时有效

Integer包装类

举个栗子:

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

这段代码输出的结果为true

使用自动装箱将基本类型转为封装类对象这个过程其实底层实现是调用封装类的valueOf方法:

Integer a =127; 相当于 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);}

如果入参 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() {}}

默认范围为:-128到127之间,范围的最大值可以通过java.lang.Integer.IntegerCache.high设置,通过for循环将范围内的数据实例化为Integer对象放到cache数组里

在测试一下:

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

输出结果为false,所以如果没有指定cache最大值时,在-128到127之间使用自动装箱时,会使用缓存

Byte包装类

再举个栗子:

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包装类的实现一致,范围也是只能在-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修饰的静态变量

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的对象

总结:

java的包装类中:Byte,Short,Integer,Long,Character使用static代码块进行初始化缓存,其中Integer的最大值可以通过java.lang.Integer.IntegerCache.high设置;Boolean使用static final实例化的对象;Float和Double直接new的对象没有使用缓存

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...