Integer的特点

学到Integer的时候都知道Integer有自动拆装箱的特性。

即Integer类型可以和int类型自动转换,这些事情都是由JDK帮我们做的。

我们知道Integer是int的包装类类型,属于引用类型。

public class AutoBox {
	public static void main(String[] args) {
		Integer i1 = 10;
		Integer i2 = 10;
		System.out.println(i1 == i2);
	}
}

这段代码我们也不陌生,但是如果完全把它当做引用类型来考虑这个问题时,答案会出乎我们的意料。

因为这才是真正的答案。

深入了解下吧!总归是好的。

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    @HotSpotIntrinsicCandidate
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

仅仅这我们还看不明白。但是即使这样也能看出来一些门道。

如果判断条件满足的话,会返回一个数组的值。

显然这和下边创建新的Integer对象有很大的区别,因为这会导致等号判断的结果天差地别。

所以再研究下IntegerCache吧。

    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * jdk.internal.misc.VM 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 =
                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() {}
    }

这回总算一清二楚了,原来在这个内部类中早就创建了一个数组,存放了从-128~127的整数值。

JDK在自动拆装箱的过程中判断我们传递的数值,如果在这个范围中,就直接拿数组中的数值,如果不在,则在内存中开辟新的内存存放我们传递的数值。

所以以下代码

public class AutoBox {
	public static void main(String[] args) {
		Integer i1 = 128;
		Integer i2 = 128;
		System.out.println(i1 == i2);
	}
}

结果是

这下毫无疑问。

猜你喜欢

转载自blog.csdn.net/yangshao2324/article/details/84302564