当前位置:网站首页>128 traps - source code analysis

128 traps - source code analysis

2022-06-22 03:40:00 Yu who wants to fly

128 trap —— Source code analysis

`public static void main(String[] args) {
		Integer aInteger = 101;
		// Automatic boxing Integer.valueOf(101)
		Integer bInteger = 101;
		System.out.println(aInteger==bInteger);
		Integer cInteger = 1001;
		Integer dInteger = 1001;
		System.out.println(cInteger==dInteger);
		int a = 101;
		// Automatic dismantling aInteger.intValue()
		System.out.println(a==aInteger);
		int b = 1001;
		System.out.println(b==cInteger);
		//Integer.valueOf(b)
		System.out.println(dInteger==b);
	}`

The result of the above code is :
 Insert picture description here

Why are the first two answers different ? This is it. Java Of 128 trap .

128 trap : Automatic packing specification requirements boolean\byte\char<=127, Be situated between -128~127 Between short and int Wrapped in a fixed object .

Why the range is -128~127 Between ? Let's take a look at the source code .

​ Auto boxing call function Integer.valueOf(101);

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






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
        VM.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() {
    }
}`

You can see in the i>=-128&&i<=127 when , Take the data directly from the cache array and return , There is no need to create a new object . Not in this range , The new Integer object , So the address points to different , That's when true and false The situation of . In the next three true in , It is because of the automatic unpacking operation .

原网站

版权声明
本文为[Yu who wants to fly]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220329265430.html