Java包装类的缓存机制原理实例详解

 更新时间:2019年12月23日 15:23:48   作者:陈明羽  
这篇文章主要介绍了Java包装类的缓存机制原理实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了Java包装类的缓存机制原理实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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的对象没有使用缓存

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java深入分析与解决Top-K问题

    Java深入分析与解决Top-K问题

    TopK问题即在N个数中找出最大的前K个,这篇文章将详细讲解三种方法解决TopK问题,文中代码具有一定参考价值,快跟随小编一起学习一下吧
    2022-04-04
  • 详解在java中进行日期时间比较的4种方法

    详解在java中进行日期时间比较的4种方法

    这篇文章主要介绍了详解在java中进行日期时间比较的4种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java操作数据库连接池案例讲解

    Java操作数据库连接池案例讲解

    这篇文章主要介绍了Java操作数据库连接池案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • MybatisX无法自动生成entity实体类的解决方法

    MybatisX无法自动生成entity实体类的解决方法

    本文主要介绍了MybatisX无法自动生成entity实体类的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • java向文件末尾添加内容示例分享

    java向文件末尾添加内容示例分享

    本文为大家提供一个java向文件末尾添加内容的示例分享,大家参考使用吧
    2014-01-01
  • 阿里SpringBoot应用自动化部署实现IDEA版Jenkins

    阿里SpringBoot应用自动化部署实现IDEA版Jenkins

    这篇文章主要为大家介绍了阿里SpringBoot应用自动化部署实现IDEA版Jenkins过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • java中最大的整数用法分析

    java中最大的整数用法分析

    这篇文章主要介绍了java中最大的整数用法,结合具体实例形式分析了java计算类java.math.BigInteger具体使用技巧,需要的朋友可以参考下
    2017-06-06
  • java 中内部类的实例详解

    java 中内部类的实例详解

    这篇文章主要介绍了java 中内部类的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • 使用ElasticSearch6.0快速实现全文搜索功能的示例代码

    使用ElasticSearch6.0快速实现全文搜索功能的示例代码

    本篇文章主要介绍了使用ElasticSearch6.0快速实现全文搜索功能,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Java获取凌晨时间戳的方法分析

    Java获取凌晨时间戳的方法分析

    这篇文章主要介绍了Java获取凌晨时间戳的方法,结合实例形式对比分析了java时间戳运算的简单操作技巧,需要的朋友可以参考下
    2018-03-03

最新评论