Java中封装类型.valueOf()

 @Test
    public void test() {
        Integer i3 =128;
        Integer i4=128;
        boolean integerGT127=i3==i4;
//false System.out.println(
"Integer 128==128"+integerGT127); Integer i1 =127; Integer i2=127; boolean integerLt127=i1==i2;
      //true System.out.println(
"Integer 127==127"+integerLt127); Integer i5 = 5001; boolean unBoxingInteger=i5 > 500 && i5 < 1000; System.out.println("Integer unbox "+integerLt127); Integer i6=500,i7=1000; boolean unBoxingInteger2=i5.compareTo(i6)>0&&i5.compareTo(i7)<0; System.out.println("Integer compare "+integerLt127); //装箱 将 10(int)转换为了 Integer Integer i9=10; //拆箱 将i9(Integer)转换为了int int i10=i9;
}

输出结果:

1 .Integer 128==128false
2 .Integer 127==127true
3 .Integer unbox true
4 .Integer compare true

为什么会出现128==128的结果?输出结果表明i1和i2指向的是同一个对象,而i3和i4指向的是不同的对象。IntegerCache 下面这段代码是Integer的valueOf方法的具体实现:

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

在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

Integer.valueOf(10)

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

Integer.valueOf(10);
Double.valueOf(23.00);
Boolean.valueOf(true);
Boolean b1=true,b2=true;
//true Boolean.valueOf(true);
// final修饰的对象只能指向唯一一个对象,不可以再将它指向其他对象,而static final修饰的对象则可以使一个常量真正做到不被修改
System.out.println(b1==b2);
// true Boolean.valueOf(true);
System.out.println(b1.equals(b2));
Double d1=20.00,d2=20.00;
// false Double.valueOf
System.out.println(d1==d2);
//true
System.out.println(d1.equals(d2));
Boolean.valueOf(true);返回了一个static final 的对象 ,final修饰的对象只能指向唯一一个对象,不可以再将它指向其他对象,而static final修饰的对象则可以使一个常量真正做到不被修改
public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code true}.
     */
    public static final Boolean TRUE = new Boolean(true);

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code false}.
     */
    public static final Boolean FALSE = new Boolean(false);

 

Double.valueOf(23.00);new一个新的对象
    public static Double valueOf(double d) {
        return new Double(d);
    }

 

posted @ 2019-04-24 19:46  暖暖-木木  阅读(929)  评论(0编辑  收藏  举报