With generics you can write a parameterized type and create instances of the type by passing a reference type or reference types. The objects will then be restricted to the type(s). For example, the java.util.List interface is generic. If you create a List by passing java.lang.String, you’ll get a List that will only accept Strings; In addition to parameterized types, generics support parameterized methods too.
The first benefit of generics is stricter type checking at compile time. This is most apparent in the Collections Framework. In addition, generics eliminate most type castings you had to perform when working with the Collections Framework.
This chapter teaches you how to use and write generic types. It starts with the ...