Java Reference
In-Depth Information
Apply type inference with the diamond operator
Understand erasure
Avoid ambiguity errors
Know generics restrictions
S ince its original 1.0 version, many new features have been added to Java. All have en-
hanced and expanded the scope of the language, but one that has had an especially pro-
found and far-reaching impact is generics because its effects were felt throughout the entire
Java language. For example, generics added a completely new syntax element and caused
changes to many of the classes and methods in the core API. It is not an overstatement to
say that the inclusion of generics fundamentally reshaped the character of Java.
The topic of generics is quite large, and some of it is sufficiently advanced to be beyond
the scope of this topic. However, a basic understanding of generics is necessary for all Java
programmers. At first glance, the generics syntax may look a bit intimidating, but don't
worry. Generics are surprisingly simple to use. By the time you finish this chapter, you will
have a grasp of the key concepts that underlie generics and sufficient knowledge to use
generics effectively in your own programs.
Generics Fundamentals
At its core, the term generics means parameterized types . Parameterized types are import-
ant because they enable you to create classes, interfaces, and methods in which the type of
data upon which they operate is specified as a parameter. A class, interface, or method that
operates on a type parameter is called generic , as in generic class or generic method .
Ask the Expert
Q :
I have heard that Java's generics are similar to templates in C++. Is this the
case?
A : Java generics are similar to templates in C++. What Java calls a parameterized type,
C++ calls a template. However, Java generics and C++ templates are not the same,
Search WWH ::




Custom Search