March 2019
Intermediate to advanced
242 pages
6h 21m
English
Generics were introduced in Java to promote type safety. It enabled developers to specify their intentions of using classes, interfaces, and collection classes with fixed types or a range of types. Violations of these intentions were enforced with compilation errors, rather than runtime exceptions, raising the compliance bar.
For example, the following shows how you would define ArrayList to store String values (repeating <String> is optional, on the right-hand side of the assignment):
List<String> names = new ArrayList<>();
However, replacing List<String> with var will put the type safety of the generics at stake:
var names = new ArrayList<>(); names.add(1); names.add("Mala"); names.add(10.9); names.add(true); ...Read now
Unlock full access