private static <T> T cloneX(T x) - What does the <T> signify here?

早过忘川 提交于 2019-12-02 00:08:52

<T> here indicates the type is implied from the arguments. So:

public static <T> List<T> createList(T... args) {
  List<T> ret = new ArrayList<T>(Arrays.asList(args));
}

can be used:

List<String> list = createList("one", "two", "three");

or

List<Integer> list2 = createList(1, 2, 3);

it just means that you will get the same class out of that method that you're putting in, to save it being Object and you having to cast all the time.

The <T> is the Type of the parameter you're passing into that generic method.

It is generic parameter. If you write then

string s = ...;
clone(s); // will be expanded to string clone(string x)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!