Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In the above declaration, what is the <T> for?

I would like to know the difference between having <T> and not having it? How does it affect the code?

like image 834
java_geek Avatar asked Dec 07 '25 06:12

java_geek


1 Answers

<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);
like image 75
cletus Avatar answered Dec 08 '25 19:12

cletus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!