Java基础 - 基本数据类型包装类

Java基本数据类型四类八种,四类:整型、浮点型、字符型、布尔型;八种:byte、short、int、long、double、float、char、boolean。

对应的包装类:Byte、Short、Integer、Long、Double、Float、Character、Boolean。

jdk1.5之后,基本数据类型和包装类之间就实现了自动拆装箱的功能。

自动装箱,原理是包装类的valueOf(xxx)方法,自动拆箱则是调用包装类的xxxValue()方法。

示例:

public class WrapperTest {

    public static void main(String[] args){
        test(100);
        test(128);
        boxingTest(100);
    }

    private static void boxingTest(int i){
        Integer num = i;
        int n = num;
    }

    private static void test(int i){
        Integer num1 = i;
        Integer num2 = i;
        System.out.println(String.format("Integer %d==%d result: " + (num1 == num2), i, i));
        System.out.println(String.format("Integer %d.equals(%d) result: " + num1.equals(num2), i, i));
    }
}

/* 执行结果
Integer 100==100 result: true
Integer 100.equals(100) result: true
Integer 128==128 result: false
Integer 128.equals(128) result: true
*/

 证明方法,编译反编译后可以得到:

public class WrapperTest
{
  public static void main(String[] paramArrayOfString)
  {
    test(100);
    test(128);
    boxingTest(100);
  }

  private static void boxingTest(int paramInt) {
    //自动装箱
    Integer localInteger = Integer.valueOf(paramInt);
    //自动拆箱
    int i = localInteger.intValue();
  }

  private static void test(int paramInt) {
    Integer localInteger1 = Integer.valueOf(paramInt);
    Integer localInteger2 = Integer.valueOf(paramInt);
    System.out.println(String.format(new StringBuilder().append("Integer %d==%d result: ").append(localInteger1 == localInteger2).toString(), new Object[] { Integer.valueOf(paramInt), Integer.valueOf(paramInt) }));
    System.out.println(String.format(new StringBuilder().append("Integer %d.equals(%d) result: ").append(localInteger1.equals(localInteger2)).toString(), new Object[] { Integer.valueOf(paramInt), Integer.valueOf(paramInt) }));
  }
}

以上示例同时引出一个基本原理:包装类的缓存值,以及包装类重写了equals方法。

下面是Integer的源代码:

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

    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

Integer缓存值范围:-128 ~ 127,源代码中也有说明:


    /**
     * 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() {}
    }

总结各包装类缓存值范围:

Byte: -128 ~ 127
Character: 0 ~ 127
Short:-128 ~ 127
Integer:-128 ~ 127
Long:-128 ~ 127
Boolean:true、false
Double和Float没有缓存值,都是直接new的对象。

猜你喜欢

转载自blog.csdn.net/aleefang/article/details/114013730