Java Reference
In-Depth Information
ing unchecked conversion in order to enable interfacing with legacy code. The
warning from unchecked conversion indicates that the generified consumer
may experience problems from heap pollution at other points in the program.
Example 4.8-1. Raw Types
Click here to view code image
class Cell<E> {
E value;
Cell(E v) { value = v; }
E get() { return value; }
void set(E v) { value = v; }
public static void main(String[] args) {
Cell x = new Cell<String>("abc");
System.out.println(x.value); // OK, has type Object
System.out.println(x.get()); // OK, has type Object
x.set("def");
// unchecked warning
}
}
Example 4.8-2. Raw Types and Inheritance
Click here to view code image
import java.util.*;
class NonGeneric {
Collection<Number> myNumbers() { return null; }
}
abstract class RawMembers<T> extends NonGeneric
implements Collection<String> {
static Collection<NonGeneric> cng =
new ArrayList<NonGeneric>();
public static void main(String[] args) {
RawMembers rw = null;
Collection<Number> cn = rw.myNumbers();
// OK
Iterator<String> is = rw.iterator();
// Unchecked warning
Collection<NonGeneric> cnn = rw.cng;
// OK, static member
}
}
In this program, RawMembers<T> inherits the method:
Search WWH ::




Custom Search