본문 바로가기

자바

Wrapper Class

Wrapper Class?

원시 타입(Primitive Type)에 대한 객체 표현을 Wrapper class라고 한다. 

 

Primitive Type -> Wrapper Class로 변환하는 것을 Boxing

Wrapper Class -> Primitive Type으로 변환하는 것을 Unboxing

 

종류

char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean

 

특징

데이터를 수정할 수 없다.

값을 변경한다면 새로운 객체를 생성해서 저장해야한다.

 

예시

Integer a = new Integer(10) // deprecated
Integer b = 10; // Auto Boxing

 

 

사용하는 이유

1. Object이므로 매개변수 전달 시 레퍼런스로 전달되어 참조에 의한 변경이 가능하다.

2. Wrapper Class에 포함된 라이브러리를 사용 가능하다.(ex intValue, parseInt 등)

2. Set, Map, List 등과 같은 Collection Framework를  사용할 때 유용하다.