๐ 1. ์๋ฐ์ ๋ฐ์ดํฐ ํ์ ๋ถ๋ฅ
Wrapped Type์ ์๊ธฐ ์ ์ Reference Type์ ๋ํด ๋จผ์ ์์๋ณด์.
์๋ฐ์์ ๋ฐ์ดํฐ ํ์ ์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ก ๋๋๋ค!
1๏ธโฃ ๊ธฐ๋ณธํ(Primitive Type) → ๊ฐ ์์ฒด๋ฅผ ์ ์ฅ
2๏ธโฃ ์ฐธ์กฐํ(Reference Type) → ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ๊ฐ์ ์ ์ฅ
โ 1. ๊ธฐ๋ณธํ (Primitive Type)
- boolean, char, byte, short, int, long, float, double
- ๊ฐ์ฒด๊ฐ ์๋๋ฉฐ, ๋ฉ๋ชจ๋ฆฌ์์ ์ง์ ๊ฐ์ ์ ์ฅ
- ์ฐ์ฐ ์๋๊ฐ ๋น ๋ฅด๊ณ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ด ํจ์จ์
โ 2. ์ฐธ์กฐํ (Reference Type)
- ๋ฐฐ์ด(int[]), ํด๋์ค(String, Integer, Double ๋ฑ), ์ธํฐํ์ด์ค, ์ด๊ฑฐํ(Enum)
- ๊ฐ์ฒด์ ์ฃผ์(์ฐธ์กฐ๊ฐ)๋ฅผ ์ ์ฅ
- ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์
โก๏ธ Wrapped Type(๋ํผ ํด๋์ค)๋ ๊ธฐ๋ณธํ์ ๊ฐ์ฒด๋ก ๊ฐ์ผ "์ฐธ์กฐํ" ๋ฐ์ดํฐ ํ์ ์ด๋ค!
๐ 2. Primitive Type (๊ธฐ๋ณธํ)
์๋ฐ์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํฐ ํ์ ์ผ๋ก, ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ ์ํด ์ง์ ๊ฐ์ ์ ์ฅ.
โ ํน์ง
- ๊ฐ์ฒด(Object)๊ฐ ์๋ → new ํค์๋ ์์ด ์ฌ์ฉ
- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ด ์ ๊ณ ์๋๊ฐ ๋น ๋ฆ
- null์ ๊ฐ์ง ์ ์์
โ ๊ธฐ๋ณธํ ํ์ ๋ชฉ๋ก
ํ์ | ํฌ๊ธฐ | ๊ธฐ๋ณธ๊ฐ | ์์ |
boolean | 1๋นํธ | false | true, false |
char | 2๋ฐ์ดํธ | '\u0000' | 'A', '1' |
byte | 1๋ฐ์ดํธ | 0 | 127, -128 |
short | 2๋ฐ์ดํธ | 0 | 32000, -32000 |
int | 4๋ฐ์ดํธ | 0 | 100, -50 |
long | 8๋ฐ์ดํธ | 0L | 100000L |
float | 4๋ฐ์ดํธ | 0.0f | 3.14f |
double | 8๋ฐ์ดํธ | 0.0d | 3.14159d |
๐ 3. Wrapped Type (๋ํผ ํด๋์ค, ๊ฐ์ฒดํ)
๊ธฐ๋ณธํ์ ๊ฐ์ฒด๋ก ๊ฐ์ผ ํด๋์ค๋ก, ์๋ฐ์ java.lang ํจํค์ง์ ํฌํจ๋์ด ์๋ค.
โ ํน์ง
- ๊ฐ์ฒด๋ผ์ null์ ๊ฐ์ง ์ ์์
- ์ปฌ๋ ์ (List, Set, Map ๋ฑ)์ ์ ์ฅ ๊ฐ๋ฅ
- ๋ฉ์๋ ์ ๊ณต (toString(), parseInt(), compareTo() ๋ฑ)
- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ํฌ๊ณ ์๋๊ฐ ๋๋ฆผ
โ ๊ธฐ๋ณธํ ↔ ๋ํผ ํด๋์ค ๋งคํ
Primitive Type | Wrapped Type |
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
๐ 4. Primitive Type vs Wrapped Type ์ฐจ์ด์
Primitive Type | Wrapped Type | |
์์ | int, double, boolean | Integer, Double, Boolean |
์ ์ฅ ๋ฐฉ์ | ์คํ(Stack)์ ์ ์ฅ | ํ(Heap)์ ์ ์ฅ (๊ฐ์ฒด) |
null ์ ์ฅ ๊ฐ๋ฅ ์ฌ๋ถ | โ ๋ถ๊ฐ๋ฅ | โ ๊ฐ๋ฅ (Integer i = null;) |
๊ธฐ๋ณธ๊ฐ | 0, 0.0, false ๋ฑ | null ๊ฐ๋ฅ |
๊ฐ์ฒด ์ฌ๋ถ | โ ๊ฐ์ฒด ์๋ | โ ๊ฐ์ฒด |
๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ | โฌ๏ธ ์ ์ | โฌ๏ธ ๋ง์ |
๋ฉ์๋ ์ ๊ณต | โ ์์ | โ Integer.parseInt(), Double.valueOf() ๋ฑ ๋ค์ํ ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ |
์ปฌ๋ ์ (List, Map) ์ฌ์ฉ | โ ๋ถ๊ฐ๋ฅ | โ ๊ฐ๋ฅ (List<Integer>) |
๐ 5. Autoboxing & Unboxing (์๋ ๋ณํ)
Java๋ Primitive Type ↔ Wrapped Type์ ์๋ ๋ณํํด ์ค๋ค!
โ ์คํ ๋ฐ์ฑ (Autoboxing)
๐ ๊ธฐ๋ณธํ → ๋ํผ ํด๋์ค (Primitive → Wrapped)
int a = 10;
Integer b = a; // ์๋ ๋ณํ (Autoboxing)
โ ์ธ๋ฐ์ฑ (Unboxing)
๐ ๋ํผ ํด๋์ค → ๊ธฐ๋ณธํ (Wrapped → Primitive)
Integer obj = new Integer(20);
int b = obj; // ์๋ ๋ณํ (Unboxing)
โ ๏ธ ์ฃผ์ํ ์
- null์ ์ธ๋ฐ์ฑ ํ๋ฉด NullPointerException ๋ฐ์!
Integer num = null;
int value = num; // โ NullPointerException ๋ฐ์!
๐6. Primitive Type vs Wrapped Type ์ธ์ ์จ์ผ ํ ๊น?
โ Primitive Type์ ์ฌ์ฉํด์ผ ํ ๋
- ์ฐ์ฐ์ด ๋ง์ ๊ฒฝ์ฐ (int, double ๋ฑ์ ๋ฉ๋ชจ๋ฆฌ ์ ๊ฒ ์ฐ๊ณ ๋น ๋ฆ!)
- ๋จ์ํ ๋ณ์ ์ ์ฅ (๊ฐ์ฒด๊ฐ ํ์ ์๋ ๊ฒฝ์ฐ)
โ Wrapped Type์ ์ฌ์ฉํด์ผ ํ ๋
- ์ปฌ๋ ์ (List, Map ๋ฑ)์ ๊ฐ์ ์ ์ฅํ ๋ (List<Integer>)
- null์ ํ์ฉํด์ผ ํ ๋ (Integer num = null;)
- ๊ฐ์ฒด๋ก์ ๋ค์ํ ๋ฉ์๋๋ฅผ ํ์ฉํด์ผ ํ ๋ (Integer.parseInt() ๋ฑ
๐ Primitive Type์ ์ฑ๋ฅ์ด ์ค์ํ ๋, Wrapped Type์ ์ปฌ๋ ์ ๊ณผ null์ด ํ์ํ ๋ ์ฌ์ฉํ๋ฉด ๋๋ค! ๐
'Backend > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
static ํค์๋ (0) | 2025.02.26 |
---|---|
JNDI๋? (0) | 2025.02.26 |
์๋ฐ(Spring) ๊ธฐ๋ณธ ์์ธ ์ฒ๋ฆฌ (0) | 2025.02.23 |
[Java] ๋น๋๊ธฐ ์์ ์ฒ๋ฆฌ (ExecutorService VS CompletableFuture) (0) | 2025.02.23 |
HttpURLConnection (0) | 2024.06.07 |