What is the point of using Java generics?

I would like to know what the first <T> in the next line of Java code represents. I read a few generics manuals, but none of the examples have 2 generics in front of the method name. Thanks.

 public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped); 
+6
source share
1 answer

The first <T> is the actual declaration of the type parameter, that is, it says that the method is general and has a parameter of type T

The second <T> is just part of the return type of the method, that is, the method returns a Provider<T> .

If the first <T> was omitted, the return type Provider<T> would be invalid, as T would not be a recognized identifier / name for the type. T recognized only as a type because the first <T> represents it as such.

+13
source

Source: https://habr.com/ru/post/908817/


All Articles