Java中Integer的parseInt和valueOf的區(qū)別詳解
Integer的parseInt和valueOf的區(qū)別
一、簡述
Integer.parseInt(s)是把字符串解析成int基本類型,Integer.valueOf(s)是把字符串解析成Integer對象類型,其實int就是Integer解包裝,Integer就是int的包裝,在jdk8中已經(jīng)自動實現(xiàn)了自動解包裝和自動包裝,所以兩種方式都能得到想要的整數(shù)值。
String str = "1"; System.out.println(Integer.valueOf(str));//1 System.out.println(Integer.parseInt(str));//1
輸出都是 1。兩個方法都可以把數(shù)字類型字符串轉(zhuǎn)成 int 類型整數(shù),但是這兩個方法還是有一點區(qū)別的:
valueOf(String s) 調(diào)用了 parseInt(String s, int radix),而 parseInt(String s, int radix) 返回值是一個 int 類型的值,之后又調(diào)用了 valueOf(int i) 將 int 進行了裝箱返回包裝類型 Integer。
所以如果不需要返回包裝類型,可以直接調(diào)用 parseInt(String s),效率更高。
二、valueOf(String s)的源碼
可以看到調(diào)用 parseInt 的時候還傳了一個 int 類型參數(shù) radix,這個參數(shù)表示進制,默認使用十進制進行轉(zhuǎn)換。下面是該方法的源碼:
public static int parseInt(String s, int radix) throws NumberFormatException { /* 警告:在初始化IntegerCache之前,VM初始化期間可能會提前調(diào)用此方法。 * 必須注意不要使用valueOf方法。 * WARNING: This method may be invoked early during VM initialization * before IntegerCache is initialized. Care must be taken to not use * the valueOf method. */ //字符串為空則拋出NumberFormatException if (s == null) { throw new NumberFormatException("null"); } //傳的進制參數(shù)小于2,拋出NumberFormatException, //并且提示進制小于最小進制 if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } //傳的進制參數(shù)大于36,拋出NumberFormatException, //并且提示進制大于最大進制 if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0; //negative 用來判斷結(jié)果是否為負數(shù) boolean negative = false; //獲取字符串長度 int i = 0, len = s.length(); //limit = -2147483647 int limit = -Integer.MAX_VALUE; //用于在添加下一位數(shù)字的前判斷是否溢出的值 int multmin; //需要追加的數(shù)字 int digit; //字符長度大于0 if (len > 0) { //判斷字符串是否有符號 char firstChar = s.charAt(0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { //第一個符號是負號,所以結(jié)果是負數(shù) negative = true; limit = Integer.MIN_VALUE; } else if (firstChar != '+') //不為負數(shù)或正數(shù),拋出NumberFormatException throw NumberFormatException.forInputString(s); //長度為1,拋出NumberFormatException if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } //計算 multmin ,注意負數(shù)和正數(shù)的limit是不一樣的, //負數(shù)的limit = -2147483648,正數(shù)的limit = -2147483647 multmin = limit / radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++), radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { //字符不為空,但是字符長度等于0,拋出NumberFormatException throw NumberFormatException.forInputString(s); } //根據(jù)正負數(shù)的標識來判斷結(jié)果取正還是取反 return negative ? result : -result; }
到此這篇關(guān)于Java中Integer的parseInt和valueOf的區(qū)別詳解的文章就介紹到這了,更多相關(guān)Integer的parseInt和valueOf的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java Springboot實現(xiàn)多文件上傳功能
這篇文章主要為大家詳細介紹了java Springboot實現(xiàn)多文件上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08Java基于API接口爬取商品數(shù)據(jù)的示例代碼
Java作為一種流行的編程語言,可以用于編寫程序來調(diào)用這些API接口,從而獲取商品數(shù)據(jù),本文將介紹如何使用Java基于API接口爬取商品數(shù)據(jù),包括請求API、解析JSON數(shù)據(jù)、存儲數(shù)據(jù)等步驟,并提供相應(yīng)的代碼示例,感興趣的朋友跟隨小編一起看看吧2023-10-10kafka 重新分配partition和調(diào)整replica的數(shù)量實現(xiàn)
當需要提升Kafka集群的性能和負載均衡時,可通過kafka-reassign-partitions.sh命令手動重新分配Partition,增加節(jié)點后,可以將Topic的Partition的Leader節(jié)點均勻分布,以提高寫入和消費速度,感興趣的可以了解一下2022-03-03SpringBoot 集成Kaptcha實現(xiàn)驗證碼功能實例詳解
在一個web應(yīng)用中驗證碼是一個常見的元素。今天給大家介紹一下kaptcha的和springboot一起使用的簡單例子。感興趣的朋友參考下吧2017-08-08