Generic Types

Generic example

/**
 * 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; }
}

Type parameter names

E - Element (used extensively by the Java Collections Framework)
K - Key
N - Number
T - Type
V - Value
S,U,V etc. - 2nd, 3rd, 4th types

Multiple Type Parameters

public interface Pair<K, V> {
  public K getKey();
  public V getValue();
}

public class OrderedPair<K, V> implements Pair<K, V> {

  private K key;
  private V value;

  public OrderedPair(K key, V value) {
    this.key = key;
    this.value = value;
  }

  public K getKey()	{ return key; }
  public V getValue() { return value; }
}

Generic Types vs. Object

MyClass<Foo> my = new MyClass<Foo>();
Foo foo = new Foo();

Foo newFoo = my.doSomething(foo);
Foo newFoo = (Foo) my.doSomething(foo); // Object typecasting

ref

What Else?
inflearn react api server -50% 할인쿠폰: 15108-f2af1e086101 buy me a coffee