Java | JDK8 | Integer

Integer 继承 抽象类Number和实现 Comparable<Integer>接口,

抽象类Number:提供拆箱的超类,可转换的基本类型有 {@code byte}, {@code double}, {@code float}, {@code int}, {@code long}, and {@code short}.

Comparable<Integer>接口:提供Integer类实现自身比较。

Integer类的最大值最小值

@Native public static final int   MIN_VALUE = 0x80000000;
@Native public static final int   MAX_VALUE = 0x7fffffff;

最小值 -2^31,最大值2^31

Integer类的缓存

  Integer的缓存值从-127开始,默认是到127,在static静态代码块进行初始化,在源码中可以看出缓存值的上限high是可以设置

static final int low = -128;
static final int high;
static final Integer cache[];
 static {
     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;

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

 

Integer类与int自动拆箱与装箱

//拆箱
 public int intValue() {
        return value;
  }

//装箱
public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
 }
原文地址:https://www.cnblogs.com/jj81/p/11509299.html