The value <T, U extends T> in the java function declaration

I see it:

public static <T,U extends T> AutoBean<T> getAutoBean(U delegate) 

I know that the input class is of type U, and the AutoBean class is of type T, and U extends T - this is the border. But what does <T, mean?

Also, if I'm going to write a function to accept the output of getAutoBean, how would you write a function declaration? (i.e. myFunction (getAutoBean (...)), what will myFunction () declare?)

Thanks!

+4
source share
5 answers

It simply declares the types your method works with. That is, basically, he must first declare the names of type types, and then use them in the signature. <T does not mean anything by itself, but the letters in angular brackets mean "These are the types that I will use in the method."

Regarding "myFunction ()" for working with getAutoBean(...) output:

 public static <T> String myFunction(AutoBean<T> arg){ // If you return a generic type object, you will also have to declare it type parameter in the first angular brackets and in angular brackets after it. // do work here } 
+2
source

<T> is the type of AutoBean that will be returned by the method. Also note that the input parameter type <U> must extend the type <T> to call the method.

+3
source

<T,U extends T> declares type parameters for the static method. This method has two types of parameters: type T and the second type U , which extends the first.

They may differ if you explicitly specify bindings for type parameters, as in

 AutoBean<Object> autoBean = Foo.<Object, String>getAutoBean("delegate"); 

Assuming getAutoBean is a member of the Foo class.

+3
source

This meant that type U should extend type T For example, these types can be used instead of T and U

 class TType { } class UType extends TType { } 

As for what <T, means, it declares that the generic type is used in the function. Here is a usage example:

 UType uType = new UType(); AutoBean<TType> autobean = getAutoBean(uType); 
+1
source

As lbolit said, declares what your method is associated with, i.e. in this case, the return type is T and the parameter is U, which is a subclass of T.

+1
source

All Articles