if (a instanceof Mailbox<Letter>) { ... }
is a troublesome construct.
Once erasure is done with it, the test compares a's raw type to the raw
type Mailbox, ignoring the parameter type (Runtime Type Inquiry, page
718 (7th ed.) or 626 (8th ed.)). For example, if a refered to a Mailbox<Blob>
instance, then the if test would be true.
1: Stack stringStk = new Stack(); 2: stringStk.push(new Integer(2)); 3: String str = (String) stringStk.pop();
indicate which statement (1, 2 or 3) is the problem and explain how generics would fix it.
The problem's with statement 2, which can essentially put a reference to an
arbitrary object into a data structure that should contain only string
references. Generics fix the problem by making sure that any reference written
into stringStk is a descendent of the type parameter (presumablbly String
in this case) (page 708 (7th ed.) or 615 (8th ed.)).
| This page last modified on 31 March 2008. |
|