> ## 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.

# Java Type Parameter

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.

```java theme={"dark"}
// 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.

```java theme={"dark"}
// 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.

```java theme={"dark"}
// 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).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
