Java

Generics

10 lessons in this section of the Java tutorial. Work through them in order, or jump to the one you need.

  1. 1.Generic Class

    A generic class in Java is a class parameterized by one or more type parameters, written as class Box<T> or class Pair<K, V>.

  2. 2.Generic Method

    A generic method is a method that declares its own type parameters, independent of any type parameters on its enclosing class.

  3. 3.Type Parameters

    Type parameters are the placeholders — written as single uppercase letters like T, E, K, V — that make Java generics work.

  4. 4.Bounded Types

    Bounded type parameters constrain which concrete types may be substituted for a type parameter.

  5. 5.Wildcards

    Wildcards — written as ? — represent an unknown type in a generic type argument position. Where a type parameter T is a name given to an unknown type that can be…

  6. 6.Upper Bound

    An upper bound in Java generics restricts a type parameter or wildcard to a specific type and all its subtypes.

  7. 7.Lower Bound

    A lower bound in Java generics restricts a wildcard to a specific type and all its supertypes.

  8. 8.Type Erasure

    Type erasure is the mechanism by which the Java compiler removes all generic type information during compilation, replacing type parameters with their erasures — either…

  9. 9.Generic Interface

    A generic interface in Java is an interface declaration that has one or more type parameters, allowing implementors and callers to bind specific types to those…

  10. 10.Raw Types

    A raw type in Java is the use of a generic class or interface without any type argument.