Private static <T> T cloneX (T x) - What does <T> mean here?

In the above description, what is <T>

for?

I would like to know the difference between having <T>

and not having it? How does this affect the code?

+2


source to share


4 answers


<T>

here indicates that 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));
}

      

:



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

      

or

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

      

+3


source


it just means that you will get the same class from that method that you insert in order to store it as Object and you have to cast it all the time.



+1


source


<T>

is the type of the parameter that you pass to this generic method.

0


source


This is a general parameter. If you write then

string s = ...;
clone(s); // will be expanded to string clone(string x)

      

0


source







All Articles