Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

Part 2: Motivation for Generic Methods in Java (Continued)

This is the second 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 this article, we present an example using overloaded methods to motivate the need for generic methods. In Part 3, we reimplement the overloaded methods from the example in this article 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.2) 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.]

The program begins by declaring and initializing three arrays—six-element Integer array integerArray (line 39), seven-element Double array doubleArray (line 40) and five-element Character array characterArray (line 41). Then, lines 43–48 output the arrays.

    When the compiler encounters a method call, it always attempts to locate a method declaration that has the same method name and parameters that match the argument types in the method call. In this example, each printArray call exactly matches one of the printArray method declarations. For example, line 44 calls printArray with integerArray as its argument. At compile time, the compiler determines argument integerArray’s type (i.e., Integer[]) and attempts to locate a method named printArray that specifies a single Integer[] parameter (lines 7–14) and sets up a call to that method. Similarly, when the compiler encounters the printArray call at line 46, it determines argument doubleArray’s type (i.e., Double[]), then attempts to locate a method named printArray that specifies a single Double[] parameter (lines 17–24) and sets up a call to that method. Finally, when the compiler encounters the printArray call at line 48, it determines argument characterArray’s type (i.e., Character[]), then attempts to locate a method named printArray that specifies a single Character[] parameter (lines 27–34) and sets up a call to that method.

    Study each printArray method. Note that the array element type appears in two locations in each method—the method header (lines 7, 17 and 27) and the for statement header (lines 10, 20 and 30). If we replace the element types in each method with a generic name—by convention we’ll use E to represent the “element” type—then all three methods would look like the one in Fig. 18.2. It appears that if we can replace the array element type in each of the three methods with a single generic type, then we should be able to declare one printArray method that can display the string representations of the elements of any array that contains objects. Note that the format specifier %s can be used to output any object’s string representation—the object’s toString method will be called implicitly. The method in Fig. 18.2 is similar to the generic printArray method declaration we discuss in Section 18.3.

Fig. 18.2 printArray method in which actual type names are replaced by convention with the generic name E.
   1   public static void printArray( E[] inputArray )
   2   {
   3   // display array elements
   4   for ( E element : inputArray )                           
   5      System.out.printf( "%s ", element );
7         System.out.println();
8   } // end method printArray
Page 1 | 2

Other Tutorials in This Series
Part 1: Introduction to Java Generics
Part 3: Generic Methods—Implementation and Compile-Time Translation

Tutorial Index