What does class mean. when we required this kind of classes. e.g.
class SimpleCounter<T> { /*...*/ }
SimpleCounter<Double> doubleCounter = new SimpleCounter<Double>();
What does class mean. when we required this kind of classes. e.g.
class SimpleCounter<T> { /*...*/ }
SimpleCounter<Double> doubleCounter = new SimpleCounter<Double>();
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; } }