原文地址:https://chenmingyu.top/java-integer-cache/

小结:

java的包装类中:

Byte,Short,Integer,Long,Character使用static代码块进行初始化缓存,其中Integer的最大值可以通过java.lang.Integer.IntegerCache.high设置;

Boolean使用static final实例化的对象;

Float和Double直接new的对象没有使用缓存

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

Integer包装类

举个栗子:

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

这段代码输出的结果为true

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

Integer a =127; 相当于 Integer a = Integer.valueOf(127);

看一下Integer的valueOf方法:

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

如果入参 i 大于等于IntegerCache.low或者小于等于IntegerCache.high),就从IntegerCache中获取对象

看一下IntegerCache:

  1. private static class IntegerCache {
  2. static final int low = -128;
  3. static final int high;
  4. static final Integer cache[];
  5. static {
  6. // high value may be configured by property
  7. int h = 127;
  8. String integerCacheHighPropValue =
  9. sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
  10. if (integerCacheHighPropValue != null) {
  11. try {
  12. int i = parseInt(integerCacheHighPropValue);
  13. i = Math.max(i, 127);
  14. // Maximum array size is Integer.MAX_VALUE
  15. h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
  16. } catch( NumberFormatException nfe) {
  17. // If the property cannot be parsed into an int, ignore it.
  18. }
  19. }
  20. high = h;
  21. cache = new Integer[(high - low) + 1];
  22. int j = low;
  23. for(int k = 0; k < cache.length; k++)
  24. cache[k] = new Integer(j++);
  25. // range [-128, 127] must be interned (JLS7 5.1.7)
  26. assert IntegerCache.high >= 127;
  27. }
  28. private IntegerCache() {}
  29. }

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

再测试一下:

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

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

Byte包装类

再举个栗子:

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

由于Byte范围在-128到127之间,所以Byte的valueOf都是从ByteCache缓存中获取的

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

ByteCache类:

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

与IntegerCache相比,ByteCache的最大值是不能修改的就是127

Short包装类

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

ShortCache类:

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

ShortCache的最大值也不可以修改,范围只能在-128 ~ 127之间

Long包装类

Long包装类的valueOf方法和LongCache类与Short包装类的实现一致,范围也是只能在-128 ~ 127之间

Character包装类

valueOf方法:

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

CharacterCache类:

  1. private static class CharacterCache {
  2. private CharacterCache(){}
  3. static final Character cache[] = new Character[127 + 1];
  4. static {
  5. for (int i = 0; i < cache.length; i++)
  6. cache[i] = new Character((char)i);
  7. }
  8. }

Character的缓存范围在0 ~ 127之间

Boolean包装类

valueOf方法:

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

TRUE跟FALSE都是static final修饰的静态变量

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

Float包装类 & Double包装类

valueOf方法:

  1. public static Float valueOf(float f) {
  2. return new Float(f);
  3. }
  4. public static Double valueOf(double d) {
  5. return new Double(d);
  6. }

Float和Double没有使用缓存,直接new的对象

总结:

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