Integer.valueof()学習の享受元モデル

4410 ワード

問題の説明:
1.Integer類初期化
//     integer       3   ,      Integer  valueof()  , Integer i=Integer.valueof(3)  
1.Integer i=3;
//                   ,         
2.Integer i=new Integer(3);
2.Integer類の比較
 
public static void test() {
        Integer a1 = 3;
        Integer b1 = Integer.valueOf(3);

        Integer a2 = 200;
        Integer b2 = Integer.valueOf(200);

        if (a1 == b1) {
            System.out.println("a1=b1");
        } else {
            System.out.println("a1!=b1");
        }
        if (a2 == b2) {
            System.out.println("a2==b2");
        } else {
            System.out.println("a2!=b2");
        }

    }
 
 
出力結果:
 
a1=b1
a2!=b2
 
 
出力結果が違っていますが、これはなぜですか?まずvalueof()のソースコードを見ます。
関連設計モード:享元モード
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

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

        //             ,     
        static final int low = -128;
        //      
        static final int high;
        //    Integer  
        static final Integer cache[];

        static {
            //         
            int h = 127;
            //         ,        jdk AutoBoxCacheMax          h,         [-128,h]。
            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;

            //        256
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                //      0-255        -128~127    ,          ,   -128-127      ,           
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }
 
このコードを説明すれば分かります。
// i  -128127  i= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
 
問題1の解釈:
1.Integer i=3;
2.Integer i=new Integer(3);
 
このようにintegerの参照を3に初期化すると、Integer類のvalueof()メソッドが自動的に呼び出されます。3はcache[256](cache[0]=-128,cache[1]=-127....cache[256]=128)配列の中で、返ったら直接3に対応する参照で、new Integer(3)を使わずに音楽を再作成することができます。
問題2説明:
説明1の原理と同じです。
        Integer a1 = 3;
        Integer b1 = Integer.valueOf(3);
        Integer a2 = 200;
        Integer b2 = Integer.valueOf(200);
Integer a 2=200としても。valueof()メソッドを呼び出しましたが、200>128の場合、cache配列に200の値がないので、new Integer()を使ってIntegerオブジェクトを新規作成するしかないです。ここでは連続して二つのオブジェクトが作成されています。彼らの参照は違っています。したがって、a 2!=b 2(==は参照)を表します。