jusung-c
으 하기싫어
jusung-c
  • 공부 기록 (96)
    • Spring (42)
      • Spring 기초 이론 (8)
      • Spring 핵심 원리 - 기본 (9)
      • Blog 만들기 with SpringBoot (25)
    • JAVA (7)
      • Java 문법 (2)
      • 객체지향의 사실과 오해 (5)
    • Algorithm (47)
      • 자료구조 (3)
      • 완전탐색 (22)
      • 정렬 (4)
      • 이분탐색 (12)
      • 투 포인터 (4)
hELLO · Designed By 정상우.
jusung-c

으 하기싫어

JAVA/Java 문법

Wrapper Class

2022. 2. 15. 18:00

 

Wrapper Class?

 

자바의 자료형은 primitive type과 reference type으로 나뉜다. 

- primitive type : char, int, float, double, boolean 등

- reference type : class, interface 등

 

기본 자료타입(primitive type) 데이터를 객체(Object)로 표현해야 할 때 객체로 다루기 위해 사용하는 클래스를 wrapper class라고 한다.

출처 : https://coding-factory.tistory.com/547

Wrapper Class 구조도

출처 : https://coding-factory.tistory.com/547

 

 

Wrapper Class를 사용하는 이유

  1. 기본 데이터 타입(primitive type)을 Object로 변환할 수 있다.
  2. parse + 기본 타입명을 이용해 문자열을 기본 타입 값으로 변환할 때 사용된다.
  3. java.util 패키지의 클래스는 객체만 처리하므로 Wrapper class가 도움이 된다.
  4. ArrayList 등과 같은 Collection 프레임 워크의 데이터 구조는 primitive type이 아닌 객체 타입만 저장하게 되고 Wrapper Class를 사용해 AutoBoxing과 AutoUnBoxing이 일어난다.
  5. 멀티스레딩에서 동기화를 지원하려면 객체가 필요하다.

 

Boxing과 UnBoxing

Boxing : Primitive 타입의 값을 Wrapper 객체로 만드는 과정

UnBoxing : Wrapper 객체에서 Primitive 타입의 값을 얻어내는 과정

public static void main(String[] args)  {
	// Boxing
    Integer num = new Integer(17);
    
    // UnBoxing
	int n = num.intValue();
}

 

AutoBoxing과 AutoUnBoxing

AutoBoxing :  Wrapper class type에 primitive type의 값이 대입될 경우

 

public static void main(String[] args)  {
	// AutoBoxing
    Integer num = 17;
    
    // AutoUnBoxing
	int n = num; 
}

 

 

출처

https://coding-factory.tistory.com/547

https://100100e.tistory.com/353

저작자표시 비영리 변경금지 (새창열림)

'JAVA > Java 문법' 카테고리의 다른 글

[Java] ArrayList와 List의 차이  (0) 2022.02.05
    'JAVA/Java 문법' 카테고리의 다른 글
    • [Java] ArrayList와 List의 차이
    jusung-c
    jusung-c

    티스토리툴바