valueOf()
方法返回相關的Number
對象,其中包含傳遞的參數的值。 參數可以是原始數據類型,字串等。
這個方法是一種靜態方法。它接收兩個參數,其中一個是String
,另一個是基數。
語法
以下是此方法的所有變體 -
static Integer valueOf(int i)
static Integer valueOf(String s)
static Integer valueOf(String s, int radix)
參數
i
- 將返回表示int
類型參數的Integer
對象。s
- 將返回字串參數的Integer
對象。radix
- 這將用於根據傳遞的String
確定返回Integer
的值。
返回值
valueOf(int i)
- 返回一個包含指定原始值的Integer
對象。valueOf(String s)
- 返回一個包含指定字串表示形式值的Integer
對象。valueOf(String s, int radix)
- 返回一個Integer
對象,該對象包含指定字串表示形式的整數值,並使用radix
值進行解析。
示例
public class Test {
public static void main(String args[]) {
Integer x =Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");
Integer b = Integer.valueOf("444",16);
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
}
}
執行上面示例代碼,得到以下結果:
9
5.0
80.0
1092
上一篇:
Java Number類
下一篇:
Java快速入門