Chapter 14 Generics
Chapter 14 Generics
15
16
Actual syntax:
public static < E > void printArrays( E[] array)
Type parameter section, also called formal
type parameters
Delimited by angle brackets ( < and > )
Precede the method’s return type
Contain one or more type parameters
19
Generic Methods: type parameter
20
1 // Fig. 18.3: GenericMethodTest.java
2 // Using generic methods to print array of different types.
Use the type parameter to declare
3
method printArray’s parameter type
4 public class GenericMethodTest
5 {
6 // generic method printArray
7 public static < E > void printArray( E[] inputArray )
8 {
9 // display array elements Type parameter section delimited
10 for ( E element : inputArray )
by angle brackets (< and > )
11 System.out.printf( "%s ", element );
12
Use the type parameter to declare method
13 System.out.println();
printArray’s local variable type
14 } // end method printArray
15
16 public static void main( String args[] )
17 {
18 // create arrays of Integer, Double and Character
19 Integer[] intArray = { 1, 2, 3, 4, 5 };
20 Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
21 Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
22
21
23 System.out.println( "Array integerArray contains:" );
24 printArray( integerArray ); // pass an Integer array
Invoke generic method printArray
25 System.out.println( "\nArray doubleArray contains:" );
with an Integer array
26 printArray( doubleArray ); // pass a Double array
27 System.out.println( "\nArray characterArray contains:" );
28 printArray( characterArray ); // pass a Character array
29 } // end main
30 } // end class GenericMethodTest
22
Generic Interface:
Comparable
public interface Comparable<T>
{
int compareTo(T other);
}
• compareTo(): Compare two objects (this and other) of type T
• Return 0 if two objects are equal
• Return -1 if this is less than other
• Return 1 if this is greater than other
• Comparable is an interface declared in java.lang.
• A class that implements Comparable defines objects that can be ordered
23
Upper bound of type parameter
24
1 // Fig. 18.5: MaximumTest.java
2 // Generic method maximum returns the largest of three objects.
3
4 public class MaximumTest Type parameter is used in the
5 { return type of method maximum
6 // determines the largest of three Comparable objects
7 public static < T extends Comparable< T > > T maximum( T x, T y, T z )
8 {
9 T max = x; // assume x is initially the largest
only object of classes that implement
10 interface Comparable can be used
11 if ( y.compareTo( max ) > 0 )
12 max = y; // y is the largest so far
13
14 if ( z.compareTo( max ) > 0 )
15 max = z; // z is the largest
16 Invokes method compareTo method
17 Comparable to compare z and max
return max; // returns the largest object
18 } // end method maximum
19
25
20 public static void main( String args[] )
21 {
22 System.out.printf( "Maximum of %d, %d and %d is %d\n\n", 3, 4, 5,
23 maximum( 3, 4, 5 ) );
24 System.out.printf( "Maximum of %.1f, %.1f and %.1f is %.1f\n\n",
25 6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
26 System.out.printf( "Maximum of %s, %s and %s is %s\n", "pear",
27 "apple", "orange", maximum( "pear", "apple", "orange" ) );
28 } // end main Invoke generic method
29 } // end class MaximumTest maximum with three strings
Maximum of 3, 4 and 5 is 5
26
Compile-Time Translation