이것 저것 개발 공부/JAVA

[JAVA] Wrapper 클래스

crushed-taro 2025. 2. 13. 15:47
728x90
반응형

1. Wrapper 클래스

Wrapper 클래스는 기본형(Primitive Type) 데이터를 객체로 감싸서 사용할 수 있도록 만든 클래스입니다.

자바에서 기본형 데이터(int, double, char, boolean 등)는 객체가 아니라서 메서드를 가질 수 없지만, Wrapper 클래스를 사용하면 객체처럼 다룰 수 있고 다양한 메서드를 활용할 수 있습니다.

2. Wrapper 클래스 종류

기본형 타입 Wrapper 클래스
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

3. Wrapper 클래스 사용법

  • 기본형 -> Wrapper 객체 (박싱, Boxing)
  • valueOf() 메서드를 사용하면 기본형 데이터를 Wrapper 객체로 변환할 수 있습니다.
public class WrapperExample {
    public static void main(String[] args) {
        Integer intObj = Integer.valueOf(100); // int → Integer
        Double doubleObj = Double.valueOf(10.5); // double → Double
        Boolean boolObj = Boolean.valueOf(true); // boolean → Boolean
        
        System.out.println(intObj); // 100
        System.out.println(doubleObj); // 10.5
        System.out.println(boolObj); // true
    }
}
  • Wrapper 객체 -> 기본형 (언박싱, Unboxing)
  • intValue(), doubleValue(), booleanValue() 등의 메서드로 기본형 데이터를 꺼낼 수 있습니다.
public class WrapperExample {
    public static void main(String[] args) {
        Integer intObj = Integer.valueOf(100);
        int intValue = intObj.intValue(); // Integer → int
        double doubleValue = intObj.doubleValue(); // Integer → double

        System.out.println(intValue); // 100
        System.out.println(doubleValue); // 100.0
    }
}

4. 오토박싱 & 오토언박싱 (AutoBoxing & AutoUnboxing)

  • 오토박싱 (AutoBoxing) -> 기본형 -> Wrapper 객체 자동 변환
public class AutoBoxingExample {
    public static void main(String[] args) {
        Integer intObj = 100; // int → Integer (자동 변환)
        Double doubleObj = 10.5; // double → Double (자동 변환)

        System.out.println(intObj); // 100
        System.out.println(doubleObj); // 10.5
    }
}
  • 오토언박싱 (AutoUnBoxing) -> Wrapper 객체 -> 기본형 자동 변환
public class AutoUnboxingExample {
    public static void main(String[] args) {
        Integer intObj = 200;
        int intValue = intObj; // Integer → int (자동 변환)

        System.out.println(intValue); // 200
    }
}

5. Wrapper 클래스의 주요 메서드

  • parseXxx() -> 문자열을 기본형 데이터로 변환
public class ParseExample {
    public static void main(String[] args) {
        int num = Integer.parseInt("100"); // "100" → int 100
        double dNum = Double.parseDouble("10.5"); // "10.5" → double 10.5
        boolean bool = Boolean.parseBoolean("true"); // "true" → boolean true

        System.out.println(num); // 100
        System.out.println(dNum); // 10.5
        System.out.println(bool); // true
    }
}
  • toString() -> 기본형 데이터를 문자열로 변환
public class ToStringExample {
    public static void main(String[] args) {
        String strNum = Integer.toString(100); // int → "100"
        String strDouble = Double.toString(10.5); // double → "10.5"

        System.out.println(strNum); // "100"
        System.out.println(strDouble); // "10.5"
    }
}

6. Wrapper 클래스 사용

  • 컬렉션(List, Set, Map)에서 기본형을 제정할 때
    • ArrayList<int>는 사용할 수 없지만, ArrayList<Integer>는 가능
  • 기본형 데이터를 객체처럼 다루고 싶을 때
  • 문자열을 숫자로 변환할 때 (parseXxx() 메서드 활용)
  • 기본형 데이터를 유틸리티 기능이 필요할 때 (Integer.toBinaryString(), Double.isNaN() 등)
728x90
반응형