首頁 > Java > java教程 > 主體

Integer.valueOf、 Integer.parseInt 、 new Integer

巴扎黑
發布: 2017-06-26 10:41:09
原創
1583 人瀏覽過

先看一下下面的結果

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選項中新增參數即可。

 

以上是Integer.valueOf、 Integer.parseInt 、 new Integer的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!