0

What does class mean. when we required this kind of classes. e.g.

class SimpleCounter<T> { /*...*/ }

SimpleCounter<Double> doubleCounter = new SimpleCounter<Double>();
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Vikas Rajak
  • 49
  • 1
  • 1
  • 7

1 Answers1

3

See From Generic Types

A generic class is defined with the following format:

class name<T1, T2, ..., Tn> { /* ... */ }
The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ..., and Tn.

To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.

With this change, the Box class becomes:

/**
    * Generic version of the Box class.
    * @param <T> the type of the value being boxed
    */
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89