Java Wrapper 클래스

자바에서는 프로그램의 성능 때문에 int와 char와 같은 기본 데이터 타입을 사용한다. 그러나 이러한 데이터는 객체가 아니기 때문에 값에 의해 메소드에 전달되고 참조에 의해 직접 전달될 수 없다. 또한, 두 메소드가 int의 값은 인스턴스를 참조할 수 있는 방법이 없다. Wrapper 클래스는 기본형 데이터 타입을 객체처럼 생성할 수 있도록 도와주는 클래스이다.

Primitive type, Reference type

Primitive type, 일반 변수 타입, 원시 타입, 기본형

  • byte, short, int, long, float, double, char, boolean
  • null 값을 가질 수 없다.
  • Stack 메모리 영역에 할당된다.
  • 실제 값을 가진다.

Reference type, wapper type, 참조형

  • Byte, Short, Integer, Long, Float, Double, Charactor, Boolean
  • null 값을 가진다.
  • new 연산자로 생성되기에 값은 Heap 메모리 영역에 할당된다.
  • 주소 값을 가진다.

Primitive type ↔ Reference type

박싱(Boxing)

  • 기본 자료형(primitive data type)을 wrapper class로 변경해주는 것을 박싱이라고 한다.
  • Primitive type → Reference type

언박싱(Unboxing)

  • Wrapper class를 기본 자료형(Primitive data type)으로 변경해주는 것을 언박싱이라고 한다.
  • Primitive type ← Reference type



최종 수정 : 2022-09-06