Wrapper Class
자바에서 Wrapper Class는 기본 데이터 타입(Primitive Type) 자료형을 객체로 다룰 수 있도록 하는 클래스입니다. 즉, 기본 데이터 타입의 값을 객체로 감싸는 역할을 합니다. Boolean, Byte, Character, Short, Integer, Long, Float, Double이 있습니다.
Wrapper Class 사용 이유
// Integer Wrapper class의 메서드/상수 기능 제공 예시
Integer num1 = 42;
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.SIZE); // 32
System.out.println(num1.toString()); // "42"
System.out.println(Integer.parseInt("42")); // 42
System.out.println(Integer.compare(1, 2)); // -1 (첫 번째 인자가 작기 때문)
1)기본형 데이터 타입을 객체로 다룰 수 있습니다.
2)null 값을 다룰 수 있습니다.
3)다양한 메서드와 상수를 제공합니다.
Wrapper Class의 객체 생성 방법
// Integer 객체 생성
Integer num1 = new Integer(42); // 기본적인 방법
Integer num2 = Integer.valueOf(42); // valueOf() 메소드를 이용한 방법
// Double 객체 생성
Double num3 = new Double(3.14); // 기본적인 방법
Double num4 = Double.valueOf(3.14); // valueOf() 메소드를 이용한 방법
// 자바 9 이후 of()를 사용해 객체 생성 가능
Integer num5 = Integer.of(42); // 자바 9 이후 버전에서 사용 가능한 방법
1)new 연산자를 이용해 객체를 생성합니다.
2)valueOf() 메서드를 이용합니다.
3)자바 9 이후에는 of() 메서드를 이용해서도 생성이 가능합니다.
'java(자바)' 카테고리의 다른 글
[java(자바)] Call by Value와 Call by Reference의 차이 _디버깅의 눈물 (0) | 2023.03.29 |
---|---|
[java(자바)] String, StringBuilder, StringBuffer의 차이 _디버깅의 눈물 (0) | 2023.03.28 |
[java(자바)] 자바의 데이터 타입-기본형(Primitive Type) vs 참조형(Reference Type) _디버깅의 눈물 (0) | 2023.03.26 |
[java(자바)] 객체 지향 프로그래밍 vs 절차 지향 프로그래밍 _디버깅의 눈물 (0) | 2023.03.25 |
[java(자바)] 객체 지향 프로그래밍 SOLID 원칙 _디버깅의 눈물 (0) | 2023.03.24 |