Java Reference
In-Depth Information
the upper bound for a type parameter and how the Java compiler uses erasure and casts to
support multiple types with generic methods and classes. We discussed how backward
compatibility is achieved via raw types. You also learned how to use wildcards in a generic
method or a generic class.
In Chapter 21, you'll learn how to implement your own custom dynamic data struc-
tures that can grow or shrink at execution time. In particular, you'll implement these data
structures using the generics capabilities you learned in this chapter.
Summary
Section 20.1 Introduction
• Generic methods enable you to specify, with one method declaration, a set of related methods.
• Generic classes and interfaces enable you to specify sets of related types.
Section 20.2 Motivation for Generic Methods
• Overloaded methods are often used to perform similar operations on different types of data.
• When the compiler encounters a method call, it attempts to locate a method declaration with a
name and parameters that are compatible with the argument types in the method call.
Section 20.3 Generic Methods: Implementation and Compile-Time Translation
• If the operations performed by several overloaded methods are identical for each argument type,
they can be more compactly and conveniently coded using a generic method. A single generic
method declaration can be called with arguments of different data types. Based on the types of
the arguments passed to a generic method, the compiler handles each method call appropriately.
• All generic method declarations have a type-parameter section (p. 843) delimited by angle brack-
ets ( < and > ) that precedes the method's return type (p. 843).
• A type-parameter section contains one or more type parameters separated by commas.
• A type parameter (p. 843) is an identifier that specifies a generic type name. Type parameters can
be used as the return type, parameter types and local variable types in a generic method declara-
tion, and they act as placeholders for the types of the arguments passed to the generic method,
which are known as actual type arguments (p. 844). Type parameters can represent only refer-
ence types.
• Type-parameter names used throughout a method declaration must match those declared in the
type-parameter section. A type-parameter name can be declared only once in the type-parameter
section but can appear more than once in the method's parameter list.
• When the compiler encounters a method call, it determines the argument types and attempts to
locate a method with the same name and parameters that match the argument types. If there's
no such method, the compiler searches for methods with the same name and compatible param-
eters and for matching generic methods.
• Objects of a class that implements generic interface Comparable ( Comparable ) can be compared
with method compareTo (p. 846), which returns 0 if the objects are equal, a negative integer if
the first object is less than the second or a positive integer if the first object is greater than the
second.
• All the type-wrapper classes for primitive types implement Comparable .
Comparable objects can be used with the sorting and searching methods of class Collections .
Search WWH ::




Custom Search