Generics in Java explain how a class or method can operate on objects of different types while keeping the code type-safe at compile time. They were added in Java 5 (J2SE 5.0) to improve safety and reduce the need for casting.
Why generics matter
- Without generics, collections like a list of Object can hold any type. When you take something out and cast it to the wrong type, you get a runtime error. Generics catch type mistakes at compile time.
- With generics, you write things like List or Map. The compiler knows what type is stored and what can be returned, so you don’t need risky casts.
How they work in simple terms
- A type parameter lets a class or method work with any type. For example, List can be a list of Strings, Integers, or any other type.
- The diamond operator (<>), introduced in Java 7, lets the compiler infer the type on the right side: List names = new ArrayList<>();.
Primitives and type safety
- Generics don’t work with primitive types directly (int, double, etc.). You use their wrapper types (Integer, Double) instead.
Type erasure and runtime behavior
- Java uses type erasure: all generic type information is removed at runtime. So List and List are both just List at run time.
- The compiler checks types, but at run time the specific type parameters aren’t available. This keeps backward compatibility and keeps runtime code simpler, but it also means you can’t rely on generic type information being present during execution.
Common differences and rules
- Generics are invariant: List
This page was last edited on 2 February 2026, at 21:48 (CET).