Sobes.tech
Junior — Middle

How did developers write code before the introduction of Generics?

sobes.tech AI

Answer from AI

Before the introduction of Generics in Java, developers used collections and other generic data structures without specifying the element types. As a result, they had to work with the Object type and perform explicit type casting when retrieving elements.

For example:

List list = new ArrayList();
list.add("string");
String s = (String) list.get(0); // explicit type casting

This led to runtime errors (ClassCastException) and reduced code readability. Generics allowed specifying the element type of collections, ensuring type safety at compile time.