Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type parameter is a placeholder identifier used in the declaration of generic classes, interfaces, and methods to represent a specific object type that will be provided at instantiation or invocation. It allows the definition of parameterized types, enforcing compile-time type safety while deferring the specification of the concrete type to the caller.

Syntax and Declaration

Type parameters are declared within angle brackets (< >). For classes and interfaces, the type parameter declaration immediately follows the type identifier. For methods, the type parameter declaration must be placed before the method’s return type. A declaration can contain one or multiple type parameters separated by commas.
// Class declaration with a single type parameter
public class Container<T> {
    private T item;
}

// Interface declaration with multiple type parameters
public interface Mapper<K, V> {
    V map(K key);
}

// Method declaration with a type parameter before the return type
public <E> void processElement(E element) {
    // Method body
}

Type Parameter vs. Type Argument

It is critical to distinguish between a type parameter and a type argument:
  • Type Parameter: The placeholder variable defined in the generic declaration (e.g., the T in public class Box<T>).
  • Type Argument: The concrete reference type provided by the caller to replace the type parameter (e.g., the String in Box<String> box = new Box<>();).
Primitive Type Restriction: Type arguments must be reference types. Primitive types (e.g., int, double, char) cannot be used as type arguments. To parameterize a generic type with a primitive value, its corresponding wrapper class (e.g., Integer, Double, Character) must be specified instead.

Bounded Type Parameters

By default, a type parameter can accept any reference type (equivalent to extending java.lang.Object). Bounds restrict the set of types that can be passed as type arguments. Upper Bounds The extends keyword is used to restrict a type parameter to a specific class or interface, and any of its subclasses or subinterfaces. Note that in the context of type parameters, extends is used for both classes and interfaces.
// T must be Number or a subclass of Number
public class MathOperation<T extends Number> {
    // ...
}
Multiple Bounds A type parameter can be constrained by multiple bounds using the ampersand (&) operator. If one of the bounds is a class, it must be specified first in the bound list. A type parameter can have at most one class bound, but multiple interface bounds.
// T must extend the Entity class AND implement the Serializable interface
public class Repository<T extends Entity & Serializable> {
    // ...
}

Standard Naming Conventions

By convention, type parameters are single, uppercase letters. This distinguishes them from standard Java class and interface names. The standard conventions are:
  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S, U, V - 2nd, 3rd, 4th types

Type Erasure

Java implements generics via a mechanism called type erasure. During compilation, the Java compiler enforces type constraints and then erases the type parameters.
  • Unbounded type parameters (e.g., <T>) are replaced with Object.
  • Bounded type parameters (e.g., <T extends Number>) are replaced with their first bound (in this case, Number).
Because of type erasure, type parameters do not exist at runtime. This means you cannot perform runtime type checks against a type parameter (e.g., if (obj instanceof T) is illegal) or instantiate a type parameter directly (e.g., new T() is illegal).
Master Java with Deep Grasping Methodology!Learn More