Some examples of the Integer int

1, the automatic boxing and unboxing mechanism:
the Java class for each packaging to provide basic data types, the corresponding relationship is as follows:
Here Insert Picture Description
the introduction of automatic packing and unpacking from Java5 mechanism, so that the basic data types and packaging can be converted to each other;

public class AutoUnboxingTest {
  public static void main(String[] args) {
	Integer a=new Integer(3);
	Integer b=3;
	int c=3;
	System.out.println("a==b:"+(a==b));//两个引用类型没有引用一个对象 结果为:false
	System.out.println("a==c:"+(a==c));//自动的拆箱操作 ,转成int进行比较 结果为true
  }
}
结果:
a==b:false
a==c:true

2、IntegerCache

public class IntegerCacheTest {
  public static void main(String[] args) {
    Integer f1=100,f2=100,f3=150,f4=150;
    System.out.println(f1==f2);
    System.out.println(f3==f4);
 }
}
结果:
true
false

Analysis:
. 1, F1, F2, F30, F4 are Integer type is a reference type
2, == is the memory address comparison reference
3, when an Integer to assign an int value, that is, when the operation will packing valueOf method calls, look at this method:

  /**
     * 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
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

Integer class is an internal IntegerCache;
source code is as follows:

/**
     * 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
     * sun.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 =
                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;
        }

        private IntegerCache() {}
    }

Summary:
1, if the integer value is not between -128-127 new target of the new Integer, Integer object but directly references the constant pool;

Published 15 original articles · won praise 7 · views 2851

Guess you like

Origin blog.csdn.net/xiaocaodeshengri/article/details/100981855