On the caching mechanism of Java Integer and other classes

Basic types of Java

(local variables are used to illustrate the situation in this paper)

  • Built in data type
  • Reference data type

Built in data type

The Java language provides eight basic types. There are six numeric types (four integers, two floating points), one character type, and one Boolean. Local variables are stored on the stack

Reference data type

In Java, variables of reference type are very similar to C/C + + pointers. The reference type points to an object, and the variable pointing to the object is the reference variable. These variables are specified as a specific type when declared, such as Employee, purchase, etc. Once a variable is declared, the type cannot be changed.

 public static void main(String[] args) {
        int int1 = 1;
        int int2 = 1;
        Integer i1 = 128;
        Integer i2 = 128;
        Integer i3 = 127;
        Integer i4 = 127;
        System.out.println("int1 == int2: 1==1-->" + (int1 == int2));
        System.out.println("i1 == i2: 128==128-->" + (i1 == i2));
        System.out.println("i3 == i4: 127==127-->" + (i3 == i4));
  }

Operation results

Caching mechanism

Integer source code, you will find that there is a static class.

  public static Integer valueOf(int i) {
	  //low= -128 
	  //int h = 127;  
	  //high = h -->high=127
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
 
    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() {}
    }
 if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);

From the above code, we can know that when the value range of Integer is - 128-127, we will go to the IntegerCache to find the corresponding value, but when the number exceeds this number, we will re create a new Integer type

For the IntegerCache type, the bottom layer is implemented with the Integer cache [] array, and the size bit of the array is new Integer[(high - low) + 1], so Integer has a cache

The default cache range for Integer is - 128 to 127, which can be changed by jvm parameters.

  • The cache upper bound high can be specified through the jvm parameter - XX:AutoBoxCacheMax=size, which takes the maximum value of the specified value and 127 and does not exceed the range represented by Integer,
  • The lower bound cannot be specified. It can only be - 128.

The caching mechanism of Integer can be understood as meta mode, Sharing element mode

Other classes with caching mechanism

In fact, not only Integer has caching mechanism, but Byte, Short, Long and Character all have caching mechanism. Let's look at the cache class in the Byte class

Other types are not checked. You can know by checking the source code
Cache range is
Integer: −128~127
Byte: −128~127
Short: −128~127
Long: −128~127
Character: 0~127 ASCII code
The internal write types are implemented by XXXCache, and the internal types are corresponding type arrays: such as Byte cache []; Integer cache[];

Keywords: Java

Added by jaret on Mon, 20 Dec 2021 00:03:44 +0200