Java Reference
In-Depth Information
they act as placeholders for the types of the arguments passed to the generic method, which
are known as
actual type arguments
. A generic method's body is declared like that of any
other method.
Type parameters can represent only reference types
—not primitive types (like
int
,
double
and
char
). Note, too, that the type-parameter names throughout the method
declaration must match those declared in the type-parameter section. For example, line 25
declares
element
as type
T
, which matches the type parameter (
T
) declared in line 22. Also,
a type parameter can be declared only once in the type-parameter section but can appear
more than once in the method's parameter list. For example, the type-parameter name
T
appears twice in the following method's parameter list:
public
static
<T> T maximum(T value1, T value2)
Type-parameter names need not be unique among different generic methods. In method
printArray
,
T
appears in the same two locations where the overloaded
printArray
meth-
ods of Fig. 20.1 specified
Integer
,
Double
or
Character
as the array element type. The
remainder of
printArray
is identical to the versions presented in Fig. 20.1.
Good Programming Practice 20.1
The letters
T
(for
“
type
”
),
E
(for
“
element
”
),
K
(for
“
key
”
) and
V
(for
“
value
”
) are commonly
used as type parameters. For other common ones, see
http://docs.oracle.com/javase/
Testing the Generic
printArray
Method
As in Fig. 20.1, the program in Fig. 20.3 begins by declaring and initializing six-element
Integer
array
integerArray
(line 9), seven-element
Double
array
doubleArray
(line 10)
and five-element
Character
array
characterArray
(line 11). Then each array is output by
calling
printArray
(lines 14, 16 and 18)—once with argument
integerArray
, once with
argument
doubleArray
and once with argument
characterArray
.
When the compiler encounters line 14, it first determines argument
integerArray
's
type (i.e.,
Integer[]
) and attempts to locate a method named
printArray
that specifies a
single
Integer[]
parameter. There's no such method in this example. Next, the compiler
determines whether there's a generic method named
printArray
that specifies a single
array parameter and uses a type parameter to represent the array element type. The com-
piler determines that
printArray
(lines 22-29) is a match and sets up a call to the method.
The same process is repeated for the calls to method
printArray
at lines 16 and 18.
Common Programming Error 20.1
If the compiler cannot match a method call to a nongeneric or a generic method declara-
tion, a compilation error occurs.
Common Programming Error 20.2
If the compiler doesn't find a method declaration that matches a method call exactly, but
does find two or more methods that can satisfy the method call, a compilation error occurs.
For the complete details of resolving calls to overloaded and generic methods, see
http://
In addition to setting up the method calls, the compiler also determines whether the
operations in the method body can be applied to elements of the type stored in the array