-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueOfExample.java
More file actions
42 lines (35 loc) · 1.26 KB
/
ValueOfExample.java
File metadata and controls
42 lines (35 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package example.UtilExample;
/**
* ValueOf() 方法
* - 可以使用 valueOf() 來把 type 轉換成想要的
*
* ! 注意 indexOf(), valueOf() 差異。
*
* Reference:
* - https://www.runoob.com/java/java-string-valueof.html
*/
public class ValueOfExample {
public static void main(String[] args) {
int2String();
String2Int();
}
public static void int2String() {
// 把 integer 轉 string
Integer val = 3;
String valString = String.valueOf(val);
System.out.println("val: " + val + "; type: " + val.getClass());
System.out.println("conver to string val: " + valString + "; type: " + valString.getClass());
System.out.println("----------------------------");
}
public static void String2Int() {
// 把 string 轉 integer
String age = "30";
Integer val = Integer.valueOf(age);
System.out.println("age: " + age + "; type: " + age.getClass());
System.out.println("conver to Integer val: " + val + "; type: " + val.getClass());
System.out.println("----------------------------");
Integer result = val + 3;
System.out.println("val: " + result + "; type: " + val.getClass());
}
// TODO: (Type) 轉換成 (Type).
}