| English | 의미 |
|---|---|
| wrapper class/ˈræpə klæs/ | 박스 클래스 |
| autoboxing/ˌɔːtəʊˈbɒksɪŋ/ | 자동 박싱 |
| unboxing/ʌnˈbɒksɪŋ/ | 자동 언박싱 |
Boxing a primitive
- Some Java structures (like
ArrayList) hold objects, not primitives. - A wrapper class 包装类 wraps a primitive in an object:
Integerwrapsint,Doublewrapsdouble. Integer boxed = Integer.valueOf(5);puts theint5inside an object.- Wrappers let primitives live where only objects are allowed.
Autoboxing and unboxing
- Java converts automatically — autoboxing 自动装箱 and unboxing 自动拆箱.
Integer x = 5;autoboxes5;int y = x;unboxes it back.- So
list.add(5)works even though the list holdsIntegerobjects. - You rarely write
Integer.valueOfby hand — autoboxing does it.
Why ArrayList needs them
ArrayList<Integer>holdsIntegerobjects, because generics require a reference type.- You can't write
ArrayList<int>— primitives aren't objects. - Autoboxing lets you add and read plain
ints conveniently. Double,Boolean, etc. play the same role for their primitives.
Compare wrappers with equals
- Two
Integerobjects should be compared with.equals(), not==. ==compares references;.equals()compares the wrapped values.- (Java caches small Integers, so
==sometimes seems to work — don't rely on it.) - The same "use
.equals()for objects" rule fromStringapplies here.
Wrappers are objects, so compare their values with .equals(), not ==. == tests reference identity; for two Integer objects with the same value it can be false. And you can't parameterize a collection with a primitive — it's ArrayList<Integer>, never ArrayList<int>. Autoboxing bridges the gap so you can still add plain ints.
Wrappers with an ArrayList:
ArrayList<Integer> nums = new ArrayList<Integer>();nums.add(5);— theint5is autoboxed into anInteger.int first = nums.get(0);— theIntegeris unboxed back toint.
A wrapper class (Integer, Double, Boolean) wraps a primitive in an object so it can go where objects are required — like an ArrayList<Integer> (you can't write ArrayList<int>). Autoboxing and unboxing convert automatically. Compare wrapper objects with .equals(), not ==.
오토박싱 및 언박싱
int ↔ Integer 변환은 자동으로 발생합니다
int를 감싸는 wrapper class는 무엇입니까?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
Integer는 int를 감싸며, Double는 double를 감쌉니다
왜 int의 ArrayList은 ArrayList
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
ArrayList
두 Integer 오브젝트는 ...와 비교해야 합니다
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
wrapper는 오브젝트이므로 값에는 .equals()를 사용해야 합니다
nums.add(5)에서 int 5는 자동으로 Integer로 박싱됩니다 (오토박싱)
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
오토박싱은 프imitives를 wrapper 오브젝트로 변환합니다
원시 데이터를 객체로 감싸는 class를 ___ class라고 합니다 (단어 하나).
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
Integer, Double, Boolean은(wrapper) class입니다.