public static < T > void PrintArray( T[] arr )
public static void PrintArray(Object[] arr)This process of replacing the type parameters with actual types is called type erasure.
public static < T extends Comparable< T > > T minimum(T x, T y, T z)
public static Comparable minimum(Comparable x, Comparable y, Comparable z)
String min = minimum("one", "three", "eight");
int m1 = minimum(6, 9, 10); double m2 = minimum(3.3, 10.5, 1.1);
public class Table< T >
private T[] list; // array variable, i.e. in a class list = (T[]) new Object[size]; // creation of array of reference variables
// Suppose the following is a generic class: public class List< T > { // ... }Then the following are valid declarations of variables and objects:
// suppose these are instance variables of a class private List< Double > dlist; private List< String > slist; // creation of objects dlist = new List< Double > (10); // List of Doubles slist = new List< String > (20); // List of Strings
public < T > void doSomething(List< T > x, T[] elements)
// this one implicitly uses Object as type argument List x = new List(5); // this one is allowed because Int is a subclass of Object (used for the // type argument of the variable y) List y = new List< Integer > (5); // this one is permitted, but less safe, since the List object is not // guaranteed to store type Double, although the variable is set that way. // Compiler will probably issue a warning List< Double > dList = new List(10);
public static double average(List< Number > x) { // ... }
public static double average(List< ? extends Number > x) { // ... }
public static void func(List< ? > x) { // ... } // any List object (with any type filled in) could be sent in
public static void func(List< ? super Number > x) { // ... } // can send in a List object instatiated with Number, or a // super-type (a parent)