Type Parameters &
Type Checking
Learn how Java uses generics and compile-time type checking to write safer, reusable code that catches errors before your program ever runs.
What Is a Type in Java?
Every variable in Java has a type that tells the compiler what kind of data it holds and what operations are allowed. Types are checked at compile time: if something doesn't match, your code won't even compile.
The Problem Without Generics
Before Java 5, collections stored everything as Object. You could put any type in, but when you took something out, you had to cast it: risky and error-prone.
Introducing Type Parameters
A type parameter (or generic) is a placeholder for a type. You declare it in angle brackets <T> and fill it in when you use the class or method. This lets one class work safely with many types.
How Compile-Time Checking Works
The Core Rule of Generics
T is not a real type: it's a placeholder the compiler fills in. After compilation, the bytecode uses Object internally (this is called type erasure).
Anatomy of a Generic Class
Generics vs Raw Types
| Generic (ArrayList<String>) | Raw (ArrayList) | |
|---|---|---|
| Type safety | Compile-time | None |
| Casting needed? | No | Yes, manual |
| Wrong-type add | Compile error | Silently allowed |
| ClassCastException? | Prevented | Possible at runtime |
| Code readability | Self-documenting | Ambiguous |
How to Apply Type Parameters
Example 1: ArrayList<String>
Example 2: Custom Generic Class
Example 3: Type Errors Caught Early
Here's what happens when you try to violate the type contract. The compiler catches every mistake before you can run the code:
Under the Hood: Type Erasure
Primitives Need Wrapper Classes
Type parameters only work with reference types, not primitives. Java provides wrapper classes that box primitives into objects. Autoboxing handles the conversion automatically.
| Primitive | Wrapper Class | Generic Usage |
|---|---|---|
| int | Integer | ArrayList<Integer> |
| double | Double | ArrayList<Double> |
| boolean | Boolean | ArrayList<Boolean> |
Common Mistakes with Generics
Summary: Key Concepts
| Concept | What It Means | Why It Matters |
|---|---|---|
| Type Parameter | Placeholder type: <T>, <E>, <K,V> | Enables reusable, type-safe code |
| Type Argument | Actual type supplied: <String> | Locks a specific usage to one type |
| Compile-time Checking | Compiler verifies all types match | Catches bugs before runtime |
| Type Erasure | Generics removed in bytecode | No runtime overhead; explains limits |
| Autoboxing | int ↔ Integer automatically | Lets primitives work with generics |
Question 1 of 5
What is the primary purpose of type parameters (generics) in Java?
Question 2 of 5
Which of the following will cause a compile-time error?
ArrayList<String> list = new ArrayList<String>(); list.add("hello"); list.add(42); // Line 3 String s = list.get(0); // Line 4
Question 3 of 5
Why can't you write ArrayList<int> in Java?
Question 4 of 5
Consider the generic class below. What does the compiler guarantee about w.getValue()?
public class Wrapper<T> { private T value; public Wrapper(T v) { value = v; } public T getValue() { return value; } } Wrapper<Double> w = new Wrapper<Double>(3.14);
Question 5 of 5
What happens to generic type information at runtime in Java?