-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoBoxing.java
More file actions
34 lines (27 loc) · 961 Bytes
/
AutoBoxing.java
File metadata and controls
34 lines (27 loc) · 961 Bytes
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
package example.WrapperClassExample;
/**
* Autoboxing and Unboxing example.
* - 整數轉成整數物件,或是整數物件轉成整數
* - 例如:整數 a 轉成 2進位字串輸出
*/
public class AutoBoxing {
public static void main(String[] args) {
int x = 5;
//! autoboxing 自動封箱
Integer xObj = new Integer(x); //! 似乎在Java 9 就被 deprecated
Integer yObj = new Integer(10);
Boolean bo = new Boolean("George");
Character ch = 'a';
System.out.println("xObj: " + xObj.getClass());
System.out.println("yObj: " + yObj.getClass());
System.out.println("The content of bo: '" + bo + "'; bo: " + bo.getClass());
System.out.println("ch: " + ch.getClass());
}
}
class Unboxing {
public void unboxing_example() {
Integer y = 10;
y = y + 20;
System.out.println("y: " + y);
}
}