<T>타입 매개변수를 의미한다.Integer)으로 고정되어 있어 재사용이 어렵다.String 타입 전용 박스가 필요하다면 다시 만들어야한다.public class Box {
private Integer item; // ⚠️ Integer 타입으로 고정
public Box(Integer item) { // ⚠️ Integer 타입으로 고정
this.item = item;
}
public Integer getItem() {
return this.item;
}
}
public class Main {
public static void main(String[] args) {
// ✅ Integer 타입 박스
Box box1 = new Box(100);
// ❌ String 타입을 저장하려면 새로운 클래스를 만들어야 함
Box box2 = new Box("ABC");
}
}
ClassCastExceptionpublic class ObjectBox {
private Object item; // ⚠️ 다형성: 모든 타입 저장 가능 (하지만 안전하지 않음)
public ObjectBox(Object item) {
this.item = item;
}
public Object getItem() {
return this.item;
}
}
public class Main {
public static void main(String[] args) {
// ✅ ObjectBox 사용
ObjectBox objBox = new ObjectBox("Hello");
String str = (String) objBox.getItem(); // 형변환 필요
System.out.println("objBox 내용: " + str); // Hello
// ⚠️ 실행 중 오류 발생 (잘못된 다운 캐스팅: ClassCastException)
objBox = new ObjectBox(100); // 정수 저장
String error = (String) objBox.getItem(); // ❌ 오류: Integer -> String
System.out.println("잘못된 변환: " + error);
}
}
✅ 제네릭
<T>(타입매개변수)
<T>(타입매개변수)는 제네릭에서 타입을 의미하는 자리이다.- 실제 데이터 타입으로 대체되어 활용 된다.
✅ 클래스에 제네릭을 활용해 재사용성과 타입안정성을 보장받을 수 있다.
제네릭 클래스는 클래스 선언부에<T>가 선언된 클래스이다.제네릭 클래스는 클래스 선언시 타입 매개변수를 사용해 다양한 데이터 타입을 안전하게 처리할 수 있는 구조이다.GenericBox<T>를 활용해서String,Integer,Double등 다양한 타입 저장 가능하다.
public class GenericBox<T> { // ✅ 제네릭 클래스
private T item;
public GenericBox(T item) {
this.item = item;
}
public T getItem() {
return this.item;
}
}
💡 타입소거(Erasure)
- 타입 소거는 컴파일 시점에 제네릭 타입 정보를 제거하는 과정이다.
<T>타입 매개변수 부분은Object로 대체된다.- 필요한 경우 컴파일러가 자동으로 강제 다운 캐스팅(cast) 코드를 삽입하여 타입 안전성을 보장한다.
public class Main {
public static void main(String[] args) {
// 1. ✅ 재사용 가능(컴파일시 타입소거: T -> Object)
GenericBox<String> strGBox = new GenericBox<>("ABC");
GenericBox<Integer> intGBox = new GenericBox<>(100);
GenericBox<Double> doubleGBox = new GenericBox<>(0.1);
// 2. ✅ 타입 안정성 보장(컴파일시 타입소거: 자동으로 다운캐스팅)
String strGBoxItem = strGBox.getItem();
Integer intGBoxItem = intGBox.getItem();
Double doubleGBoxItem = doubleGBox.getItem();
System.out.println("strGBoxItem = " + strGBoxItem);
System.out.println("intGBoxItem = " + intGBoxItem);
System.out.println("doubleGBoxItem = " + doubleGBoxItem);
}
}
<T> 가 선언된 메서드이다.public class GenericBox<T> {
// 속성
private T item;
// 생성자
public GenericBox(T item) {
this.item = item;
}
// 기능
public T getItem() {
return this.item;
}
// ⚠️ 일반 메서드 T item 는 클래스의 <T> 를 따라갑니다.
public void printItem(T item) {
System.out.println(item);
}
// ✅ 제네릭 메서드 <S> 는 <T> 와 별개로 독립적이다.
public <S> void printBoxItem(S item) {
System.out.println(item);
}
}
public class Main {
public static void main(String[] args) {
GenericBox<String> strGBox = new GenericBox<>("ABC");
GenericBox<Integer> intGBox = new GenericBox<>(100);
// ⚠️ 일반메서드: 클래스 타입 매개변수를 따라갑니다.
// String 데이터 타입 기반으로 타입소거가 발생.
// String 타입의 다운캐스팅 코드 삽입!
strGBox.printItem("ABC"); // ✅ String 만 사용가능
strGBox.printItem(100); // ❌ 에러 발생
// ✅ 제네릭 메서드: 독립적인 타입 매개변수를 가집니다.
// String 타입 정보가 제네릭 메서드에 아무런 영향을 주지 못함.
// 다운캐스팅 코드 삽입되지 않음.
strGBox.printBoxItem("ABC"); //✅ 모든 데이터 타입 활용 가능
strGBox.printBoxItem(100); //✅ 모든 데이터 타입 활용 가능
strGBox.printBoxItem(0.1); //✅ 모든 데이터 타입 활용 가능
}
}
✅ 자바에서
<T>제네릭이 활용된 곳
Optional<T>와ArrayList<T>컬렉션 클래스 등은 제네릭 클래스이다.- 여기서
<T>는 실제 데이터 타입으로 대체되어 활용된다.