Article From:

Automatically disassemble and pack boxes:Java The process by which the compiler automatically converts the original type to the encapsulated class is called autoboxing, which is equivalent to calling the valueOf method.

Integer a = 10;
Integer b = Integer.valueOf(10);
System.out.println(a == b);  // true

Introducing Integer’s caching mechanism through a demo

public class IntegerCacheDemo {
    public static void main(String[] args) {

        Integer integer1 = 127;
        Integer integer2 = 127;
        if (integer1 == integer2) {
            System.out.println("integer1 == integer2");

        } else {
            System.out.println("integer1 !== integer2");

        Integer integer3 = 128;
        Integer integer4 = 128;

        if (integer3 == integer4) {
            System.out.println("integer3 == integer4");

        } else {
            System.out.println("integer3 !== integer4");
Operation results:Integer1== integer2
integer3 !== integer4

Although the “==” sign is used to compare two objects, it seems that Integer1 and Integer2 are not the same, but there is also Integer’s caching mechanism, the two are actually equal. As mentioned above, automatic boxing is equivalent to calling the valueOf method, so let’s look at valueOSource code of F method

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

Before creating a new Integer object, it is looked up in IntegerCache. cache. There is a special Java class responsible for Integer caching

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

Javadoc Detailed description of this class is used to achieve caching support, and support the automatic boxing process between – 128 and 127. The maximum 127 can be modified by the JVM startup parameter – XX: AutoBox CacheMax = size. The cache passes throughA for loop implementation. Create as many integers as possible from small to large and store them in an array of integers called cache. This cache is initialized when the Integer class is first used. Later, you can use instance objects contained in the cache instead ofIs to create a new instance (in the case of automatic packing).

In fact, when introducing this feature in Java 5, the scope is fixed – 128 to + 127. Later in Java 6, the Max was mapped to java. lang. Integer. IntegerCache. high, which could beUse JVM startup parameters to set the maximum. This allows us to adjust flexibly according to the actual situation of the application to improve performance.

This caching behavior applies not only to Integer objects. We have a similar caching mechanism for all integer-type classes.

There is ByteCache for caching Byte objects

ShortCache is used to cache Short objects

There is LongCache for caching Long objects

CharacterCache is used to cache Character objects

Byte,Short,Long There is a fixed range: – 128 to 127. For Character, the range is 0 to 127. Except Integer can change the range of parameters, nothing else can.



Reference material:

Leave a Reply

Your email address will not be published. Required fields are marked *