对基本类型包装类常量池的补充

时间:2022-07-23
本文章向大家介绍对基本类型包装类常量池的补充,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在前文JAVA-常量及常量池中,对java中的各种常量池进行分析。在具体的细节中,并不完善。现在对数据类型包装类常量池进行相应的补充。有很多细节也是面试中容易遗漏的地方。

IntegerCache

IntegerCache的源码位于Intger包中。

/**
 * Cache to support the object identity semantics of autoboxing for values between
 * -128 and 127 (inclusive) as required by JLS.
 *
 * The cache is initialized on first usage.  The size of the cache
 * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 * During VM initialization, java.lang.Integer.IntegerCache.high property
 * may be set and saved in the private system properties in the
 * sun.misc.VM class.
 */

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

作为Integer类的内部类。这段注释非常关键。意思是说,IntegerCache对-128-127之间的数据自动装箱支持。在IntegerCache第一次使用的时候通过static的构造方法进行初始化。可以通过-XX:AutoBoxCacheMax=设置IntegerCache缓存的大小。java.lang.Integer.IntegerCache.high可以指定IntegerCache支持的最大值。

LongCache

LongCache没有IntegerCache这么复杂,不能通过参数对LongCache进行修改。

private static class LongCache {
    private LongCache(){}

    static final Long cache[] = new Long[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Long(i - 128);
    }
}

作为Long包装类的内部类,从初始化开始,就实现了[-128 - 127]之间的范围。

ShortCache

ShortCache与LongCache类似,作为内部类:

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));
    }
}

也是在初始化的时候就定义了这个缓存。

ByteCache

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));
    }
}

在初始化的过程中就定义了[-128 - 127]这个范围的缓存。同样也不支持任何方式的调整。

CharacterCache

而对于Character支持的范围就变成了0-127.

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);
    }
}

BooleanCache

boolean是一种特殊情况,没有采用上面这种内部类的方式。由于boolean只有两个结果。那么在Boolean类内部 :

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

定义了两个表示true和false的内部类。这样在比较的过程中,所有的Boolean实际上所有boxed的对象最终都只有这两个类。

总结

对于java中的8种基本的数据类型,存在Cache的情况如下:

原始类型

缓存类型

范围

是否可调整

int

IntegerCache

-128-127

通过-XX:AutoBoxCacheMax=可以设置,改变上限,下限不可调整

long

LongCache

-128-127

不可调整

short

ShortCache

-128-127

不可调整

byte

ByteCache

-128-127

不可调整

char

CharacterCache

0-127

不可调整

boolean

内部类TRUE/FALSE

true/false

不可调整

float

double

java在装箱和拆箱的时候,都会使用其valueof方法。来强制让这些操作走缓存。这样可以节省大量的内存空间。需要注意的是:

  • GC对常量池cache缓存没有任何影响。除非类从jvm卸载。
  • -XX:AutoBoxCacheMax= 参数设置对JVM client模式无效,只对sever模式有效。

在系统中的数字在一个较小的范围内变化的时候,可以通过缓存的方式,提前创建好。