java Integer.valueOf 和 Integer.parseInt 和 new Integer區別及注意事項

先看一下下面的結果java

1.System.out.println(127==127); //true , int type compare
2.System.out.println(128==128); //true , int type compare
3.System.out.println(new Integer(127) == new Integer(127)); //false, object compare
4.System.out.println(Integer.parseInt("128")==Integer.parseInt("128")); //true, int type compare
5.System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); //true ,object compare, because IntegerCache return a same object
6.System.out.println(Integer.valueOf("128")==Integer.valueOf("128")); //false ,object compare, because number beyond the IntegerCache
7.System.out.println(Integer.parseInt("128")==Integer.valueOf("128")); //true , int type compare

 

解釋數組

int整型常量比較時,== 是值比較,因此1,2返回true。1,2是值比較。緩存

new Integer() 每次構造一個新的Integer對象,因此3返回false。3是對象比較。函數

Integer.parseInt每次構造一個int常量,因此4返回true。4是值比較。源碼分析

Integer.valueOf返回一個Integer對象,默認在-128~127之間時返回緩存中的已有對象(若是存在的話),因此5返回true,6返回false。5,6是對象比較。this

第7個比較特殊,是int 和 Integer之間的比較,結果是值比較,返回true。spa

 

總結code

對於整型的比較,首先判斷是值比較仍是對象比較,值比較確定返回true,有一個是值就是值比較。對象比較,則看對象是怎麼構造出來的,若是是採用new Integer方式,則每次產生新對象,兩個new出來的Integer比較確定返回false,若是是Integer.valueOf方式的話,注意值的區間是否在-128~127之間,若是在,則構造的相同值的對象是同一個對象,==比較後返回true,不然返回false。orm

因此,對於值比較==放心沒問題,對於Integer的比較最好用equals方法比較對象內容,固然注意先判斷Integer是否不爲null。對象

 

知識擴展

針對Integer.valueOf源碼分析一下

1.咱們調用的Integer.valueOf方法, 它先調用parseInt轉成int型數值,再調它本身的重載方法

public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

2.Integer.valueOf重載方法,根據數值i的大小,決定是否從緩存中取一個Integer對象

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

3.Integer的構造函數,很是簡單

  public Integer(int value) {
        this.value = value;
    }

4.IntegerCache靜態類,是Integer的內部類,三個屬性(一個緩存的Integer型數組+一組緩存範圍)

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"); //讀取VM參數 if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue); //配置值轉換成int數值
                    i = Math.max(i, 127);  //和127比較,取較大者 // Maximum array size is Integer.MAX_VALUE(控制緩存數組的大小,最大爲整型的最大值,這樣一來,h值就必須小於整型最大值,由於要存 -128~0這129個數嘛)
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1); //實際就是 h=Math.min(i,Integer.MAX_VALUE-129),正整數能緩存的個數
                } 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++);  //將一些int常量緩存進Integer對象數組緩存中去 // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127; //若是小於127,拋異常
        }

        private IntegerCache() {}
    }

 看完上述Integer.valueOf源碼後,你就會發現,默認的Integer緩存int常量池是能夠配置的,配置方法是添加VM參數,加: -Djava.lang.Integer.IntegerCache.high=200

Intellij IDEA 運行配置的VM Options選項中添加參數便可。

相關文章
相關標籤/搜索