Part 3: Generic MethodsImplementation and
Compile-Time Translation (Continued)
This is the third in a series of three articles that shows how to declare and use generic methods in Java Standard Edition 5.0 (Java SE 5.0). In Part 1, we introduced the concept of generics. In Part 2, we presented an example using overloaded methods to motivate the need for generic methods. In this article we reimplement the overloaded methods from the example in Part 2 using a single generic method. These articles are intended for students who are already familiar with Java and for Java developers.
Download the code examples for this tutorial here.
[Note: This series of three articles (Part 1, Part 2, Part 3) is an excerpt (Section 18.1) of Chapter 18, Generics, from our textbook Java How to Program, 6/e. These articles may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.870-876. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.] Line 7 begins method printArray’s declaration. All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method’s return type (< E > in this example). Each type parameter section contains one or more type parameters (also called formal type parameters), separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type, parameter types and local variable types in a generic method declaration, and 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. Note that 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 10 declares element as type E, which matches the type parameter (E) declared in line 7. 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 E appears twice in the following method’s parameter list:public static < E > void printTwoArrays( E[] array1, E[] array2 )
Type parameter names need not be unique among different generic methods.
|
|
|
Method printArray’s type parameter section declares type parameter, E, as the placeholder for the array element type that printArray will output. Note that E appears in the parameter list as the array element type (line 7). The for statement header (line 10) also uses E as the element type. These are the same two locations where the overloaded printArray methods of Fig. 18.1 specified Integer, Double or Character as the array element type. The remainder of printArray is identical to the versions presented in Fig. 18.1.
|
As in Fig. 18.1, the program begins by declaring and initializing six-element Integer array integerArray (line 19), seven-element Double array doubleArray (line 20) and five-element Character array characterArray (line 21). Then the program outputs each array by calling printArray (lines 24, 26 and 28)—once with argument integerArray, once with argument doubleArray and once with argument characterArray.
Other Tutorials in This Series
Part 1: Introduction to Java Generics
Part 2: Motivation for Generic Methods in Java

