August 2017
Intermediate to advanced
440 pages
10h
English
Programmers often need a way to specify that a collection only contains elements of a particular type, such as Int, Student, or Car. Without generics, we would need to separate classes for each data type (IntList, StudentList, CarList, and so on). Those classes would have a very similar internal implementation, which would only differ in the stored data type. This means that we would need to write the same code (such as adding or removing an item from a collection) multiple times and maintain each class separately. This is a lot of work, so before generics were implemented, programmers usually operated on a universal list. This forced them to cast elements each time they were accessed:
// Java ArrayList list = new ArrayList(); ...
Read now
Unlock full access