Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Medium · Free

What are generics? Why List<String> is safer than raw List?

FresherFundamentalsOOP & Language Fundamentals
Model answer

Generics

Generics add compile-time type parameters so collections are type-safe without casting. Raw types erase to Object and cause ClassCastException at runtime.

java
List<String> names = new ArrayList<>();
names.add("Ada");
// names.add(42); // compile error
Mention type erasure lightly—you can't do new T() without tricks.