Android基础Java 基础

Java 常用类 03. Java 整数缓冲区

2021-12-16  本文已影响0人  yjtuuige

问题:

public class Main {
    public static void main(String[] args) {
        Integer n1=new Integer(100);
        Integer n2=new Integer(100);
        System.out.println(n1==n2);
        Integer n3=100;    // 自动装箱
        Integer n4=100;
        System.out.println(n3==n4);
        Integer n5=200;    // 自动装箱
        Integer n6=200;
        System.out.println(n5==n6);
    }
}

原因:

分析:

public class Main {
    public static void main(String[] args) {
        Integer n1=new Integer(100);
        Integer n2=new Integer(100);
        System.out.println(n1==n2);
        // 自动装箱,Integer.valueOf() 为隐藏代码
        Integer n3=Integer.valueOf(100);
        Integer n4=Integer.valueOf(100);
        System.out.println(n3==n4);
        // 自动装箱,Integer.valueOf() 为隐藏代码
        Integer n5=Integer.valueOf(200);
        Integer n6=Integer.valueOf(200);
        System.out.println(n5==n6);
    }
}
@IntrinsicCandidate
    public static Integer valueOf(int i) {
        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 Integer[] archivedCache;

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    h = Math.max(parseInt(integerCacheHighPropValue), 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            // Load IntegerCache.archivedCache from archive, if possible
            CDS.initializeFromArchive(IntegerCache.class);
            int size = (high - low) + 1;

            // Use the archived cache if it exists and is large enough
            if (archivedCache == null || size > archivedCache.length) {
                Integer[] c = new Integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new Integer(j++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }
public class Main{
    public static void main(String[] args) {
        Integer a[]={100};
        Integer n3=a[0];
        Integer n4=a[0];
        System.out.println(n3==n4);
        Integer n5=new Integer(200);
        Integer n6=new Integer(200);
        System.out.println(n5==n6);
    }
}
上一篇下一篇

猜你喜欢

热点阅读