Java version 5 introduced generics, language features for declaring and using type-agnostic classes and interfaces. When working with Java's collections framework (which I introduce in Chapter 8), these features help you avoid java.lang.ClassCastExceptions.

NOTE: Although the main use for generics is the collections framework, Java's class library also contains generified (retrofitted to make use of generics) classes that have nothing to do with this framework: java.lang.Class, java.lang.ThreadLocal, and java.lang.ref.WeakReference are three examples.

This section introduces you to generics. You first learn how generics promote type safety in the context of the collections classes, and then you explore generics in the contexts of generic types and generic methods.

Was this article helpful?

0 0

Post a comment