[英]What is “Integer.valueOf().intValue()” supposed to do?
這是我無法理解的java代碼行。
String line = "Some data";//I understand this line
int size;//I understand this line too
size = Integer.valueOf(line,16).intValue();//Don't understand this one
我所知道的是Integer.ValueOf(行 )與Integer.parseInt(行)相同,是不是這樣? 如果我錯了,請糾正我; 謝謝。
Integer.ValueOf(line,16)
將字符串值line
轉換為Integer
對象。 在這種情況下,基數為16。
intValue()
從上面創建的Integer
object
獲取int
值。
此外,上述兩個步驟相當於Integer.parseInt(line,16)
。
要獲得更多信息,請參閱Integer類的Java API文檔。
是的,這相當於:
size = Integer.parseInt(line, 16);
實際上,在查看實現時,現有代碼實際上是有效實現的:
size = Integer.valueOf(Integer.parseInt(line, 16)).intValue();
這顯然毫無意義。
順便說一下,前一行中-1的賦值是沒有意義的。 只有在Integer.parseInt
拋出異常時仍然可以讀取該值才有意義,但由於size
的范圍與調用Integer.valueof
塊相同,因此在異常后它不會在范圍內無論如何。
請查看左側變量的數據類型。
public class Test {
public static void main(String[] args) {
String s = "CAFE";
Integer m = Integer.valueOf(s, 16);
int n = m.intValue();
System.out.println(n);
}
}
Integer
是一個包裝int
的引用類型,它是一種基本類型。
" = Integer.valueOf().intValue()"
和示例:
String myNumber = "54";
int c = Integer.valueOf(myNumber).intValue(); // convert strings to numbers
結果:
54 // like int (and before was a String)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.