Box<(타입변수)T> : 지네릭 클래스, 'T의 Box' 또는 'T Box'라고 읽는다
T : 타입변수 또는 타입 매개변수. (T는 타입문자)
Box : 원시 타입(raw type) // 일반클래스 -> 지네릭 클래스
ArrayList<Tv> list = new ArrayList<Tv>(); // 일치
ArrayList<Product> list = new ArrayList<Tv>(); // 에러, 불일치
List<Tv> list = new ArrayList<Tv>(); // 다형성, 일치
List<Tv> list = new LinkedList<Tv>(); // 다형성, 일치
ArrayList<Product> list = new ArrayList<Product>();
list.add(new Product());
list.add(new Tv()); // ok, Product 상속
list.add(new Audio()); // ok, Product 상속
import java.util.*;
class Product{}
class Tv extends Product{}
class Audio extends Product{}
public class Ex {
public static void main(String[] args) {
ArrayList<Product> productList = new ArrayList<Product>();
ArrayList<Tv> tvList = new ArrayList<Tv>();
// ArrayList<Product> tvList = new ArrayList<Tv>(); // 에러
// List<Tv> tvList = new ArrayList<Tv>(); // OK, 다형성
productList.add(new Tv());
productList.add(new Audio());
tvList.add(new Tv());
tvList.add(new Tv());
// tvList.add(new Audio()); // 컴파일 에러
printAll(productList);
// printAll(tvList); // 컴파일 에러
}
static void printAll(ArrayList<Product> list) {
for(Product p : list) {
System.out.println(p);
}
}
}