*기본 자료형을 객체 타입으로 변환해야하는 경우 ▪️ 객체로 저장해야할 경우 ▪️ 매개변수로 객체가 요구될 경우 (ex. *제네릭, Collecion 타입) ▪️ 객체간 비교가 필요할 경우
*제네릭 : 모든 종류의 타입을 다룰 수 있도록 일반화된 타입 매개변수로 클래스나 메서드를 선언하는 기법 * 오토 박싱, 오토 언박싱: JDK 1.5부터는 자바 컴파일러가 알아서 자동 처리해줌 편의성을 위해 오토 박싱과 언박싱이 제공되고 있지만, 내부적으로 추가 연산 작업이 거치게 된다. 따라서, 오토 박싱&언박싱이 일어나지 않도록 동일한 타입 연산이 이루어지도록 구현하자. 100만건 기준으로 약 5배의 성능 차이가 난다. 따라서 서비스를 개발하면서 불필요한 오토 캐스팅이 일어나는 지 확인하는 습관을 가지자.
publicclassAutoBoxingExample{
publicstaticvoidmain(String[] args){
// 기본 타입 int를 Integer 객체로 자동 변환 (AutoBoxing)
Integer integerObj = 10; // int -> Integer
System.out.println("AutoBoxing: " + integerObj);
}
}
publicclassAutoUnBoxingExample{
publicstaticvoidmain(String[] args){
// Integer 객체를 int 기본 타입으로 자동 변환 (AutoUnBoxing)
Integer integerObj = new Integer(20);
int num = integerObj; // Integer -> int
System.out.println("AutoUnBoxing: " + num);
}
}