Chapter 2. Generics
NOTEIn this chapter:
- Using Type-Safe Lists
Using Type-Safe Maps
Iterating Over Parameterized Types
Accepting Parameterized Types as Arguments
Returning Parameterized Types
Using Parameterized Types as Type Parameters
Checking for Lint
Generics and Type Conversions
Using Type Wildcards
Writing Generic Types
Restricting Type Parameters
Without any further ado, I'm going to dive right into the deep end of the
pool. More than any other feature, Tiger (or whatever version it ends up
being labeled as) brings to the
table generics. While the name might
throw you, generics actually bring a greater degree of type safety to Java
than anything you could imagine. It's finally possible to create parameterized
types, lists that only accept Strings, and ditch all that annoying
class-casting code. Even better, you can limit types that your custom
classes and methods accept, removing a huge amount of tedious errorchecking
and type-checking code.Additionally, generics are foundational to many of the other features specific
to Tiger. Generics have a bearing on varargs, annotations, enumerations,
collections, and even some of the new concurrency utilities of the
language. While you may want to browse through other parts of this book,
you'd do well to take your time and really work through this chapter, lab
by lab. There, that's enough introduction for a few chapterslet's get to it.