Integer IntegerCache source code reading

  • 2020-12-20 03:34:39
  • OfStack

First, take a look at the test results in section 1:


/*public static void main(String[] args) {
    Integer a = 128, b = 128;
    Integer c = 127, d = 127;
    System.out.println(a == b);//false
    System.out.println(c == d);//true

  }*/
  
  /*public static void main(String[] args) {
    Integer int1 = Integer.valueOf("100");
    Integer int2 = Integer.valueOf("100");
    System.out.println(int1 == int2);//true
  }*/

  public static void main(String[] args) {
    Integer int1 = Integer.valueOf("300");
    Integer int2 = Integer.valueOf("300");
    System.out.println(int1 == int2);//false
  }

JDK's source code is as follows:


public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));
  }

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

There is another mystery in it. There is another IntegerCache 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 originally instantiated the -128 to 127 (tunable) integers ahead of time.

So that explains the answer, it turns out that no matter how many Integer you create in this range, ValueOf comes out with the same object.

But why does JDK want so much of this? If we think about it carefully, most of the products on Taobao are priced within 100. One day, the background server will use new for Integer. If you use IntegerCache, the time of new will be reduced and the efficiency will be improved. At the same time, JDK also provides high worth configurable in cache,

This definitely increases the flexibility to optimize JVM.

conclusion

Above is the entire content of Integer IntegerCache source code reading, I hope to be helpful to you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: