S
●
Create a generic method
●
Create a generic constructor
●
Create a generic interface
●
Utilize raw types
●
Apply type inference with the diamond operator
●
Understand erasure
●
Avoid ambiguity errors
●
Know generics restrictions
ince its original 1.0 version, many new features have been added to Java. All have
enhanced and expanded the scope of the language, but one that has had an especially
profound and farreaching 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 book. 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
important 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
Dostları ilə paylaş: