Replace type parameters by their erasures, normally The two fragments are alternatives, not consecutive declarations in one scope. For a user-defined
Object or the first declared bound, and insert checked casts where an erased result is used as its former specific type. A simple generics in Java example becomes:// Generic version
Vector<String> names = new Vector<String>();
names.add("Ada");
String s = names.get(0);
// Non-generic version
Vector names = new Vector();
names.add("Ada");
String s = (String) names.get(0);Box<T>, replace fields, parameters and returns of type T by Object, and cast the retrieved result. Bounded parameters instead erase to their bound. A full translation must also preserve method dispatch where erasure changes overridden signatures, using suitable forwarding methods; this is the principle of type erasure, not simply deleting every pair of angle brackets.The raw collection permits accidental insertion of an unrelated object, which may fail much later at a cast. The generic collection rejects such insertion during compilation, documents its contract at the declaration, and eliminates repetitive casts at use sites. The generic version catches more errors earlier and expresses the programmer's intended element type directly. Generic types are largely erased at runtime, so their advantage is static checking rather than a new runtime vector representation. The translation mechanism is specified by the Java language specification.
Articles by others on the same topic
There are currently no matching articles.