12.4 Generics

There are no favorites in my office. I treat them all with the same general inconsideration.

—LYNDON B. JOHNSON, 36th President of the United States.

As we mentioned earlier, beginning with version 5.0, Java allows class definitions to include parameters for data types. These parameters are called generics. This section gives a brief introduction to this topic. Programming with generics can be subtle and requires care. To do serious programming with generics, you may want to consult a more advanced text.

The Basics

Classes and methods can use a type parameter instead of a specific data type. When Generics allows you to parameterize a data type using such a class or method, a programmer plugs in any class type for the type parameter ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.