-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava142_Wrapper.java
More file actions
72 lines (55 loc) · 1.63 KB
/
Copy pathJava142_Wrapper.java
File metadata and controls
72 lines (55 loc) · 1.63 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package java0907_api;
/*
* 1. 기본자료형을 클래스로 정의해놓은 Wrapper라 한다.
* char -> Character
* byte -> Byte
* short -> Short
* int -> Integer
* long -> Long
* float -> Float
* double -> Double
* boolean -> Boolean
*
* 2. 메모리에 저장된 값을 다른 자료형으로 변환해주는 메소드를 제공하기 위해서 Wrapper클래스를 제공한다.
*
* 3. auto boxing과 auto unboxing은 jdk5.0에서 추가된 기능이다.
* auto boxing => 스택 -> 힙영역에 복사
* auto unboxing => 힙 -> 스택영역에 복사
*
*/
public class Java142_Wrapper {
public static void main(String[] args) {
String data = "1234";
// String -> Integer
Integer it = new Integer(data);
// Integer -> int
int num = it.intValue();
System.out.println("num=" + num);
// Integer -> double
double pro = it.doubleValue();
System.out.println("pro=" + pro);
// Integer -> long
long nn = it.longValue();
System.out.println("nn=" + nn);
// String -> Float
Float ft = new Float(data);
// Float -> int
int fx = ft.intValue();
System.out.println("fx=" + fx);
// Float -> double
double de = ft.doubleValue();
System.out.println("de=" + de);
// String -> int
int num1 = Integer.parseInt(data);
System.out.println("num1=" + num1);
// String -> double
double num2 = Double.parseDouble(data);
System.out.println("num2=" + num2);
int x = 10; // stack
// Integer ig = new Integer(x); // heap
Integer ie = x; // auto boxing (stack -> heap)
// int k = ie.intValue(); // heap->stack
int y = ie; // auto unboxing (heap -> stack)
System.out.println("y=" + y);
}
}