Deitel & Associates, Inc. Logo

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

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

Part 3: Generic Methods—Implementation 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.]

    When the compiler encounters line 24, 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 is no such method in this example. Next, the compiler determines whether there is a generic method named printArray that specifies a single array parameter and uses a type parameter to represent the array element type. The compiler determines that method printArray (lines 7–14) is a match and sets up a call to the method. The same process is repeated for the calls to method printArray at lines 26 and 28.

Common Programming Error 18.2
If the compiler cannot match a method call to a non-generic or a generic method declaration, a compilation error occurs.
Common Programming Error 18.3
If the compiler does not find a method declaration that matches a method call exactly, but does find two or more generic methods that can satisfy the method call, a compilation error occurs.

    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 argument. The only operation performed on the array elements in this example is to output the string representation of the elements. Line 11 performs an implicit toString call on every element. To work with generics, every element of the array must be an object of a class or interface type. Since all objects have a toString method, the compiler is satisfied that line 11 performs a valid operation for any object in printArray’s array argument. The toString methods of classes Integer, Double and Character return the string representation of the underlying int, double or char value, respectively.

    When the compiler translates generic method printArray into Java bytecodes, it removes the type parameter section and replaces the type parameters with actual types. This process is known as erasure. By default all generic types are replaced with type Object. So the compiled version of method printArray appears as shown in Fig. 18.4—there is only one copy of this code that is used for all printArray calls in the example. This is quite different from other, similar mechanisms, such as C++’s templates in which a separate copy of the source code is generated and compiled for every type passed as an argument to the method. As we will discuss in Section 18.4, the translation and compilation of generics is a bit more involved than what we have discussed in this section.


Fig. 18.4 Generic method printArray after erasure is performed by the compiler.
     1   public static void printArray( Object[] inputArray )
2 {
3 // display array elements
4 for ( Object element : inputArray )
5 System.out.printf( "%s ", element );
6
7 System.out.println();
8 } // end method printArray

    By declaring printArray as a generic method in Fig. 18.3, we eliminated the need for the overloaded methods of Fig. 18.1, saving 20 lines of code and creating a reusable method that can output the string representations of the elements in any array that contains objects. However, this particular example could have simply declared the printArray method as shown in Fig. 18.4 using an Object array as the parameter. This would have yielded the same results because any Object can be output as a String. In a generic method, the benefits become apparent when the method also uses a type parameter as the method’s return type, as we demonstrate in the next section.

Page 1 | 2 | 3

Other Tutorials in This Series
Part 1: Introduction to Java Generics
Part 2: Motivation for Generic Methods in Java

Tutorial Index