| static Integer valueOf(int i) | 回傳參數的整數物件 |
| static Integer valueOf(String s) | 回傳參數的整數物件 |
| static Integer valueOf(String s, int radix) | 回傳參數的字串物件 |
舉例如下
lass ValueOfDemo {
public static void main(String[] args) {
int i1 = 22;
Integer a1 = Integer.valueOf(i1);
System.out.println(a1);
String s1 = "33";
Integer a2 = Integer.valueOf(s1);
System.out.println(a2);
Integer a3 = Integer.valueOf(s1, 16);
System.out.println(a3);
}
}
/* 《程式語言:教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:ValueOfDemo.java
功能:示範物件導向的基本觀念
作者:張凱慶
時間:西元 2010 年 10 月 */編譯後執行,結果如下

其中,第 4 行
Integer a1 = Integer.valueOf(i1);
這是直接將 int 型態的 i1 轉換成 Integer 型態的物件。
第 8 行
Integer a2 = Integer.valueOf(s1);
這是將字串中所含的整數轉換成 Integer 型態的物件。
第 11 行
Integer a3 = Integer.valueOf(s1, 16);
這是將字串中所含的整數,以十六進位的模式轉換成 Integer 型態的物件。
| 中英文術語對照 | |
|---|---|
| 物件 | object |
| 字串 | string |
沒有留言:
張貼留言