WEB/JAVA

Wrapper 클래스 개념과 사용하는 이유

디벨로펀 2020. 7. 20. 23:19

Wrapper 클래스

자바의 primitive type의 객체화 버전이다.

비교PrimitiveWrapper

1 byte Byte
2 short Short
3 int Integer
4 long Integer
5 float Integer
6 double Integer
7 char Character
8 boolean Boolean
  • 박싱 : 기본 타입 -> 래퍼 클래스의 형태로 변환
  • 언박싱 : 래퍼 클래스 -> 기본 타입의 형태로 변환
  • 자바 1.5 버전 이상부터는 박싱/언박싱이 자동으로 됨

사용이유

int [] arr = new int[4]; 
ArrayList<int> list = new ArrayList<>() -----> Error 
ArrayList<Integer> list = new ArrayList<>()
  • Integer로 받는 경우에만 ArrayList 사용 가능
  • 함수 파라미터가 Object인경우 Class형태인 Wrapper로 넘겨야함

활용법

  • Integer.MAX_VALUE, Integer.MIN_VALUE 를 통해 int 범위의 최대 최소값 접근
  • <-- 객체에 포함된 함수 활용이 가능함 -->
  • Integer.toBinaryString(), Integer.toHexString(i), Integer.toOctalString(i)를 통해 2진수, 8진수, 16진수의 문자열로 변환가능
  • 각 래퍼 클래스의 에서 longValue(), doubleValue(), byteValue() 등을 통해 기본형 타입의 값으로 바꿀 수 있다.

 

 

https://heepie.me/155

 

[2017.10.19] 16. 왜 Wrapper 클래스를 사용할까?

결론적으로 wrapper 클래스는 사용하는 "기본 자료형을 클래스화하므로 클래스의 장점을 갖게 하기 위함" 이다. 구분 예시(Chocolate) Wrapper 클래스 그림 목적 외부로부터 오염을 방지해 유통이 가능�

heepie.me

https://knoc-story.tistory.com/20

 

Wrapper Class를 사용하는 이유

Wrapper Class를 사용하는 이유 - 기본 자료형에 대해 객체로서 인식되도록 '포장'을 하기 위함 1) 매개변수로 객체가 요구될 때 2) 기본형 값이 아닌 객체로 저장해야 할 때 3) 객체간의 비교가 필요��

knoc-story.tistory.com

https://cheonjoosung.github.io/blog/java-wrapper

 

Joo's IT

Java 예외처리 try catch finally 사용법 Java

cheonjoosung.github.io

https://jusungpark.tistory.com/17

 

래퍼 클래스(wrapper class)

자바는 기본형(primitive type), 참조형(reference type) 으로 나뉘는 것은 모두가 아는 사실. 자바를 공부했다 해도 wrapper class가 뭔지 모르는 사람이 은근히?! 있음.. 그래서 오늘은 참조형(reference typ..

jusungpark.tistory.com