티스토리 뷰
Wrapper 클래스
포장, 기본 자료형 데이터를 포장한다.
기본 자료형 byte, short, int, long, char, float, double, boolean은 객체가 아니다.
이런 자료형들을 객체로 사용해야할 때가 종종 생김.
이 때 wrapper 클래스를 사용한다.
wrapper 클래스는 내부적으로 아래와 같이 구성되어 있음.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Integer extends Number implements Comparable<Integer> { | |
..... | |
private final int value; | |
..... | |
} |
기본 자료형을 그냥 감싼 듯한 모양새이다.
wrapper 클래스를 생성할 때는
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Integer a = new Integer(3); | |
Double b = new Double(2.5); | |
Character c = new Character('a'); |
와 같이 작성하면 된다. 그리고int intValue = a.intValue();
형태로 기본 자료형 변환이 가능하다.
오토박싱
기본 자료형을 wrapper 클래스로 감싸지 않아도 wrapping 해준다. 자동으로 캐스팅 해준다. 자동으로 객체처럼 만들어준다.Integer a = 3;
이라고 하면 Integer a = new Integer(3); 으로 자동 변환되는 것
오토언박싱
반대로 기본 자료형으로 바꾸지 않아도 자동으로 포장이 풀린다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Integer a = new Integer(3); | |
int i = a; //내부적으로 int i = a.intValue(); 동작 |

728x90
'#2 > JAVA' 카테고리의 다른 글
[JAVA] 문자(열)을 요리조리~~ split, charAt, toCharArray() (0) | 2022.12.23 |
---|---|
[JAVA] Optional 이란? + Iterable (0) | 2022.11.09 |
[Java/Spring] static을 사용해보자. (0) | 2022.06.30 |
[java] 인터페이스 interface 에 관한 간단한 생각 (0) | 2021.12.30 |
casting, parsing 캐스팅, 파싱 (0) | 2021.11.08 |