Box<Object> objBox = null;
Box box = (Box)objBox; // OK. 지네릭 타입 -> 원시 타입. 경고 발생
objBox = (Box<Object>)box; // OK. 원시타입 -> 지네릭 타입. 경고 발생
JDK1.5부터 지네릭스가 나왔기 때문에, 이후로는 원시 타입은 쓰지 않는 것이 좋다.
예제12-3-2
class Ex12_3_2 {
public static void main(String[] args) {
Box b = (Box)new Box<String>();
b.add(new Integer(100));
Box<String> bStr = null;
b = (Box)bStr; // Box<String> -> Box 가능 but 경고
bStr = (Box<String>)b; // Box -> Box<String> 가능 but 경고
} // main
}
// 매개변수로 FruitBox<Fruit>, FruitBox<Apple>, FruitBox<Grape> 등이 가능
static Juice makeJuice(FruitBox<? extends Fruit> box) { ... }
FruitBox<? extends Fruit> box = new FruitBox<Fruit>(); // OK
FruitBox<? extends Fruit> box = new FruitBox<Apple>(); // OK
예제12-3-3
class Ex12_3_3 {
public static void main(String[] args) {
FruitBox<? extends Fruit> fbox = (FruitBox<? extends Fruit>)new FruitBox<Fruit>();
// FruitBox<Apple> -> FruitBox<? extends Fruit> 가능
FruitBox<? extends Fruit> abox = new FruitBox<Apple>();
// FruitBox<? extends Fruit> -> FruitBox<Apple> 가능? 가능!
FruitBox<Apple> appleBox = (FruitBox<Apple>)abox; // OK. 경고발생 - 형변환 생략불가
} // main
}