0

I was wondering what static <T> means in this context? or is it <T> void?

I know what static and void both mean but I'm not sure what <T> means here

 static <T> void sort(List<T> list, Comparator<? super T> c)
sinelaw
  • 16,205
  • 3
  • 49
  • 80
TheRapture87
  • 1,403
  • 3
  • 21
  • 31
  • 1
    It is generic method https://docs.oracle.com/javase/tutorial/extra/generics/methods.html. In short `T` represents type which will be inferred based on what parameters you will pass to method, which lets you pass `List` or `List` or others. – Pshemo Apr 15 '15 at 17:06
  • Ok so what does super T> mean? I thought super is used to call the parent's constructor before calling the current class's? – TheRapture87 Apr 15 '15 at 17:09
  • You should read tutorial about generics https://docs.oracle.com/javase/tutorial/extra/generics/index.html first. It is all explained there. If you will have problems with some part of tutorial then ask question and explain what confuses you. – Pshemo Apr 15 '15 at 17:09
  • @Aceboy1993 `super` has many meanings in different contexts. Here is `? super T` any type to which `T` can be assigned. e.g. `Number n = new Integer(1);` `T` is `Integer` and `? super T` in this case is `Number` – Peter Lawrey Apr 15 '15 at 17:10
  • or try this already answered related SO questions, http://stackoverflow.com/questions/490091/java-generics, http://stackoverflow.com/questions/7815528/what-are-generics-in-java – Shrikant Havale Apr 15 '15 at 17:11

1 Answers1

2

The <T> means that there is a generic T being used in this declaration. This has two impacts;

  • The T in the first type of the first argument must match the type in the second argument. (And ? super T means it must be a super class or interface of T) Without the use of a generic, there is no way to do this.
  • The type can be given explicitly with a statement like Collections.<Integer>sort(list, myComparator);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130