T is a type parameter that will be replaced by a real type when an object of type Gen is created. : Generic Parameter « Generics « Java






T is a type parameter that will be replaced by a real type when an object of type Gen is created.

  
class Gen<T> {
  T ob;

  Gen(T o) {
    ob = o;
  }

  T getob() {
    return ob;
  }

  void showType() {
    System.out.println("Type of T is " + ob.getClass().getName());
  }
}

class GenDemo {
  public static void main(String args[]) {
    Gen<Integer> iOb;

    iOb = new Gen<Integer>(88);

    iOb.showType();

    int v = iOb.getob();
    System.out.println("value: " + v);

    System.out.println();

    Gen<String> strOb = new Gen<String>("Generics Test");

    strOb.showType();

    String str = strOb.getob();
    System.out.println("value: " + str);
  }
}

   
  








Related examples in the same category

1.A simple generic class with two type parameters: T and V.A simple generic class with two type parameters: T and V.
2.Java generic: Hierarchy argumentJava generic: Hierarchy argument
3.Boxing Generic Example
4.Demonstrate a raw generic type. Demonstrate a raw generic type.
5.Create a generic class that can compute the average of an array of numbers of any given type.
6.the type argument for T must be either Number, or a class derived from Number.
7.Demonstrate a raw type.
8.A subclass can add its own type parameters.
9.Compare two generic parameters
10.Default implementation of {@link java.lang.reflect.ParameterizedType}
11.Get the Generic definition from a class for given class with given index.