래퍼(Wrapper) 클래스
기본형의 한계
int, double 같은 기본형(Primitive type)
객체가 아님. 객체 참조가 필요한 컬렉션 프레임워크 사용 불가.
메서드 제공할 수 없음
자바의 컬렉션 프레임워크는 내부적으로 객체의 참조 주소를 저장하기 때문이다. 그래서 int, double 같은 기본형은 직접 저장할 수 없다.
제네릭 사용할 수 없다
null 값을 가질 수 없다. 데이터가 없음 이라는 상태를 나타낼 수 없다.
자바는 기본형에 대응하는 래퍼 클래스를 기본으로 제공
byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
다음과 같은 특징을 가지고 있다.
불변이다
equals로 비교해야 한다.
Integer integerObj = Integer.valueOf(10); //
내부에서 new Integer(10)을 사용해서 객체를 생성하고 돌려
-128 ~ 127 자주 사용하는 숫자 값 재사용, 불변
ㅐ해당 범위의 값을 조회하면 미리 생성된 Integer 객체를 반환한다. 해당 범위의 값이 없으면 new Integer()를 호출한다.
무조건 equals로 비교, ==은 참조값 비교임
래퍼 클래스 생성 - 박싱(boxing)
기본형을 래퍼 클래스로 변경하는 것을 마치 박스에 물건을 넣은 것 같다고 해서 박싱(Boxing)이라고 한다.
new Integer(10)은 직접 사용하면 안됨. deprecated 문법
Integer.valueOf(10). 성능 최적화 기능. 마치 문자열 풀과 비슷하게 자주 사용하는 숫자를 미리 생성해두고 재사용함
integerObj.intValue(); -> 언박싱 (Unboxing)
래퍼 클래스는 객체이기에 == 비교를 하면 인스턴스의 참조값을 비교한다.
래퍼 클래스는 내부의 값을 비교하도록 equals()를 재정의 해두었다. 따라서 값을 비교하려면 equals()를 사용해야 한다.
자바에서 int를 Integer로 변환하거나, Integer를 int로 변환하는 부분 정리
int value = 7;
Integer boxedValue = Integer.valueOf(value);
int unboxedValue = Integer.intValue();
래퍼 클래스 - 오토 박싱, 오토 언박싱
int value = 7;
Integer boxedValue = value; // 오토 박싱(Auto-boxing)
int unboxedValue = boxedValue; // 오토 언박싱(Auto-Unboxing)
컴파일러가 컴파일 단계에서 위 변환 코드를 추가 해줌.
String 변환
Integer.valueOf("10");
Integer.parseInt("10");

✅ Integer.parseInt(String s)
— 문자열 → int
Integer.parseInt(String s)
— 문자열 → int🔹 정의 위치
// java.lang.Integer
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s, 10); // 기본은 10진수
}
🔹 내부 구현 (radix 포함 버전)
public static int parseInt(String s, int radix) throws NumberFormatException {
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else if (firstChar == '+') {
i++;
}
if (i == len) {
throw new NumberFormatException("For input string: \"" + s + "\"");
}
multmin = limit / radix;
while (i < len) {
// 문자 → 숫자 변환
digit = Character.digit(s.charAt(i++), radix);
if (digit < 0) {
throw new NumberFormatException("For input string: \"" + s + "\"");
}
if (result < multmin) {
throw new NumberFormatException("Integer overflow");
}
result *= radix;
if (result < limit + digit) {
throw new NumberFormatException("Integer overflow");
}
result -= digit;
}
} else {
throw new NumberFormatException("For input string: \"" + s + "\"");
}
return negative ? result : -result;
}
🔍 핵심 특징
부호 판별:
+
,-
체크오버플로우 방지:
result
가limit
보다 작아지는지 수시로 체크**
Character.digit(c, radix)
**로 문자 → 숫자 변환음수로 누적 계산 후, 마지막에
-result
로 바꿔서 반환
✅ Integer.intValue()
— Integer
객체 → 기본형 int
Integer.intValue()
— Integer
객체 → 기본형 int
public final class Integer extends Number implements Comparable<Integer> {
private final int value;
@Override
public int intValue() {
return value;
}
}
Class 클래스는 클래스의 정보(메타데이터)를 다루는데 사용된다. Class 클래스를 통해 개발자는 실행 중인 자바 애플리케이션 내에서 필요한 클래스의 속성과 메서드에 대한 정보를 조회하고 조작할 수 있다.
Class 클래스의 주요 기능
타입 정보 얻기 : 클래스의 이름, 슈퍼클래스, 인터페이스 , 접근 제한자 등과 같은 정보를 조회
리플렉션 : 클래스에 정의 된 메서드, 필드 , 생성자 등을 조회하고, 이들을 통해 객체 인스턴스를 생성하거나 메서드를 호출하는 등의 작업을 할 수 있다.
동적 로딩과 생성 : Class.forName() 메서드를 사용하여 클래스를 동적으로 로드하고, newInstance() 메서드를 통해 새로운 인스턴스를 생성할 수 있다.
애노테이션 처리 : 클래스에 적용된 애노테이션(annotation)을 조회하고 처리하는 기능을 제공한다.
Class 클래스의 주요 기능
Class clazz = String.class;
getDeclaredFields(): 클래스의 모든 필드를 조회한다.
getDeclaredMethods(): 클래스의 모든 메서드를 조회한다.
getSuperclass(): 클래스의 부모 클래스를 조회한다.
getInterfaces(): 클래스의 인터페이스들을 조회한다.
System 클래스
시스템과 관련된 기본 기능들을 제공한다.
Math, Random 클래스
Last updated