JDK source code parsing Integer class uses Flyweight mode

JDK source code analysis

The Integer class uses the Flyweight pattern.

Let's look at the following example first:


public class Demo {
    public static void main(String[] args) {
        Integer i1 = 127;
        Integer i2 = 127;
​
        System.out.println("i1和i2对象是否是同一个对象?" + (i1 == i2));
​
        Integer i3 = 128;
        Integer i4 = 128;
​
        System.out.println("i3和i4对象是否是同一个对象?" + (i3 == i4));
    }
}

Run the above code, the results are as follows:

Why does the first output statement output true and the second output statement output false?

Decompile by decompiling software, the code is as follows:

public class Demo {
    public static void main(String[] args) {
        Integer i1 = Integer.valueOf((int)127);
        Integer i2 Integer.valueOf((int)127);
        System.out.println((String)new StringBuilder().append((String)"i1\u548ci2\u5bf9\u8c61\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff1f").append((boolean)(i1 == i2)).toString());
        Integer i3 = Integer.valueOf((int)128);
        Integer i4 = Integer.valueOf((int)128);
        System.out.println((String)new StringBuilder().append((String)"i3\u548ci4\u5bf9\u8c61\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff1f").append((boolean)(i3 == i4)).toString());
    }
}

As you can see from the code above, the bottom layer of the operation of directly assigning the basic data type to the variable of the Integer type is used  valueOf() , so you only need to look at this method.

public final class Integer extends Number implements Comparable<Integer> {
    
    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 {
            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) {
                }
            }
            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() {}
    }
}

You can see that by  default  , the number of  objects in between is  created and cached first  , Integer-128 ~ 127Integer

When calling  valueOf , if the parameter is  -128 ~ 127 between, the subscript is calculated and returned from the cache,

Otherwise create a new  Integer object.

Guess you like

Origin blog.csdn.net/qq_39368007/article/details/114024824