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

來源:互聯網
上載者:User

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

先看一下下面的結果

1.System.out.println(127==127); //true , int type compare2.System.out.println(128==128); //true , int type compare3.System.out.println(new Integer(127) == new Integer(127)); //false, object compare4.System.out.println(Integer.parseInt("128")==Integer.parseInt("128")); //true, int type compare5.System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); //true ,object compare, because IntegerCache return a same object6.System.out.println(Integer.valueOf("128")==Integer.valueOf("128")); //false ,object compare, because number beyond the IntegerCache7.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是對象比較。

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

 

總結

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

所以,對於值比較==放心沒問題,對於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選項中添加參數即可。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.