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);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM