Chapter 2. Generics
Note
In this chapter:
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 String
s, 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 chapters—let’s get to it.
Using Type-Safe Lists
One of Java’s greatest strengths is its typing. Everything is an ...
Get Java 5.0 Tiger: A Developer's Notebook 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.