generics
14 tutorials tagged “generics”.
- Bounded Type Parameters in JavaRestrict a type parameter with extends so you can call its methods: T extends Number, T extends Comparable<T>, and multiple bounds with &.
- Diamond Operator in JavaThe <> that saves repeating type arguments: how the compiler fills it in, using it with anonymous classes, and the trap of combining it with var.
- Bridge Methods in JavaHidden methods the compiler adds so overriding still works after type erasure: why compareTo(Object) appears in your class, and how to see it.
- Generic Classes in JavaWrite your own generic class: declare a type parameter, use it in fields and methods, create typed instances, and use several parameters like Pair<K, V>.
- Generic Constructors in JavaA constructor with its own type parameter, independent of the class: how to write one, when it's useful, and how it differs from a generic class.
- Generic Interfaces in JavaInterfaces with type parameters: a Repository<T, ID> contract, implementing it with real types or keeping it generic, and the JDK's Comparable<T>.
- Generic Methods in JavaA method with its own type parameter: the <T> before the return type, how the compiler infers T from the arguments, and when to give it explicitly.
- Restrictions of Generics in JavaWhat you can't do with generics, why each rule exists because of type erasure, the exact compile errors, and the standard workaround for each.
- Heap Pollution and SafeVarargsWhen a variable of a generic type points to an object of the wrong type: how varargs and raw types cause it, the warnings, and when @SafeVarargs is honest.
- Generics in JavaWhy generics exist: a list that accepts anything fails at runtime, a typed list fails at compile time. Type parameters, their names, and what you gain.