Generic type T in java

  1. Type T is considered to be a reference type. So primitive types can not be passed in if a generic type T is required unless primitive type can be auto boxing to its object type.
  2. special types such as T[], since T is a reference type, array passed in must be boxed typed array or array containing objects. For example,
var list = Arrays.asList(1, 2, 3);
// toArray(T[])
list.toArray(new Integer[0]); // cannot pass in new int[0]

int arr[] = {1, 2, 3};
// asList(T [])
List<int[]> l = Arrays.asList(arr); // the whole arr will be considered as one object
Integer arr2[] = {1, 2, 3}; // elems will auto boxing
List<Integer> l2 = Arrays.asList(arr2); // as arr2's type is T[], arr2 is regarded as varargs.