Generics in Java Created 2026-10-13 Updated 2026-11-05
Generics in Java parameterize classes, interfaces and methods by reference types so reusable code can retain compile-time type checks. For example,
List<String> describes a list whose elements are strings, and retrieving one has static type String. Java generic types are generally invariant: List<Integer> is not a subtype of List<Number>. Wildcards express related producer or consumer views. Type erasure means a parameterized class usually has one shared runtime class rather than a fresh class for each type argument. Primitive values use wrapper types in ordinary generic collections.[ref]https://docs.oracle.com/javase/tutorial/java/generics/. Oracle Java tutorial, Generics
// Target: computer-science.bigb
Java (programming language) Created 2026-10-13 Updated 2026-11-05
Java is a class-based, statically typed programming language whose ordinary implementation compiles source into bytecode for a Java virtual machine. Its type system, exception rules and object model specify program behaviour; the virtual machine supplies execution and managed storage. Generics in Java add parameterized types checked largely at compilation, with type erasure shaping their runtime representation. The Java language, the virtual-machine specification and a particular vendor's development kit are separate layers, so an implementation detail should not be mistaken for a universal language rule.[ref]
// Target: computer-science.bigb
Past exam of the mathematics course of the University of Cambridge 2007 ia Paper 5 3 a Solution Created 2026-09-24 Updated 2026-10-14
A generic class, interface or method in Java is parameterized by one or more types. Generics in Java allow one implementation of a collection or algorithm to be reused with different reference types while checking the allowed values and results statically. For example:The type parameter is declared inside angle brackets, as in
Vector<String> names = new Vector<String>();
names.add("Ada");
String first = names.get(0);
class Box<T> {
private T value;
public void put(T x) { value = x; }
public T get() { return value; }
}Box<T>, and supplied as a type argument, as in Box<String>. A generic method places its parameter list before its return type, for example static <T> T identity(T x). Bounds such as <T extends Number> restrict type arguments, and wildcards such as ? extends Number or ? super Integer describe restricted views. Primitive values require wrapper types such as Integer rather than a type argument int. Generics provide reusable code with compile-time checking of element types. Past exam of the mathematics course of the University of Cambridge 2007 ia Paper 5 3 b Solution Created 2026-09-24 Updated 2026-10-14
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.