DEVELOP/CONCEPT
primitive type vs wrapper class
콘순이
2025. 4. 11. 19:48
Primitive Type
원시 타입이라고도 하며, 자바의 기본 자료형, 실제 값 자체를 저장한다.
int, boolean, char, double 등이 여기에 해당한다.
Wrapper Class
Primitive Type을 객체로 감싼 클래스이다.
Integer, Boolean, Character, Double 등이 여기에 해당한다.
주요 차이점
항목 | Primitive Type | Wrapper Class |
저장 방식 | 값 자체 저장 | 객체로 저장 (레퍼런스 타입) |
Null 허용 | X | O |
기본값 | 0, false 등 | null |
컬렉션/제네릭 사용 | X | O (List<Integer> 가능) |
메서드 | X | O (Integer.parseInt(), toString() 등) |
성능 | 빠름 | 상대적으로 느림 |
자바에서는 primitive와 wrapper 간 자동 변환이 일어난다.
int a = 10;
Integer b = a; // 오토박싱: int → Integer
int c = b; // 언박싱: Integer → int
언제 써야할까
- 일반 계산, 반복문 등 : primitive
- 컬렉션에 저장할 때 : wrapper
- null 처리가 필요할 때 : wrapper
- 객체로 넘겨야 할 때 : wrapper