Java Generics Basics
Fast recall
Quick revision
Core answer
List or Box. They let the compiler check how values are used, remove many manual casts, and let one implementation work with several reference types. Java implements most generic typing with erasure, but class files can still contain generic signature metadata and the compiler may add casts or bridge methods.Whether you understand generics provide compile-time safety and what problem they replaced
Saying generics improve runtime performance, or not knowing pre-generics code needed casts
Mention type erasure: generics are a compile-time feature, erased to Object/bounds at runtime
Interview-ready guide
Interview answer
Generics let you parameterize types, so instead of a plain list you declare a List of String, and the compiler knows exactly what that list can hold.
The main reason they exist is compile-time type safety. If I try to put an Integer into a List of String, that's a compile error, caught immediately, rather than a crash later. Before generics, which arrived in Java 5, collections just held Object.
So you could put anything in, and when you read an element out you had to cast it back to the type you expected. If you cast wrong, you got a ClassCastException at runtime, often in production. Generics move that check from runtime to compile time, which is exactly where you want bugs caught.
A second benefit is that you no longer need casts when reading — get returns the real type directly, so the code is cleaner. And a third benefit is reusability: I can write one generic class, like a Box of T, or a generic method, and it works for any type while staying type-safe. One important detail that impresses interviewers is type erasure.
Generics are purely a compile-time feature. After compilation, the type parameter is erased — it becomes Object, or its bound — so there's no generic type information in the bytecode at runtime. That's why you can't write new T or use instanceof with a type parameter.
So to sum up: generics give compile-time safety, remove casts, and enable reusable code, and they're implemented through erasure, not at runtime.
Java Generics Basics
Direct Answer
Generics parameterize classes, interfaces, and methods—for example, List<String> or Box<T>. They let the compiler check how values are used, remove many manual casts, and let one implementation work with several reference types. Java implements most generic typing with erasure, but class files can still contain generic signature metadata and the compiler may add casts or bridge methods.
Code Example
// One generic class works for any type, type-safely:
class Box<T> {
private T value;
public void set(T value) { this.value = value; }
public T get() { return value; }
}
Box<String> sb = new Box<>(); sb.set("hi"); String x = sb.get();
Box<Integer> ib = new Box<>(); ib.set(7); int y = ib.get();
The same Box<T> definition serves String, Integer, or any other type without duplication.
Common Mistakes & Pitfalls
- Thinking generics speed up runtime. They're compile-time safety; bytecode is the same after erasure.
- Using raw types.
List list = new ArrayList()drops all safety and brings back casts. - Expecting runtime type info. Because of erasure you can't do
new T()orinstanceof T. - Forgetting the diamond. Use
new ArrayList<>()(Java 7+) instead of repeating the type.
Check your understanding
Answer these without looking back at the article.
- What is type erasure in Java generics?
- What is the difference between <? extends T> and <? super T>?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.