What does '< T >' mean in “< T > void someMethod()”? [closed]

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

What does mean in void someMethod() ? what return type does such function have?

回答1:

is not a return type, void is. in this case represents a type variable which is used in this method only. For example, if I write this:

 T getFirstValue(List list) {     return list.get(0); } 

this means that if I give the list containing objects of some specific type, it will return an object of this exact type. For example, if I give it a List, I know that I'll get back a String. The compiler guesses that T is actually a String so the method will act as

String getFirstValue(List list) {     return list.get(0); } 


回答2:

Here, T is the generic argument. See the tutorial.

The return type is void, since the method's signature is void someMethod().



回答3:

Where T is a normal generic object representation in java.

The type parameter section, delimited by angle brackets (), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ..., and Tn.

Defining Methods

The return type―the data type of the value returned by the method, or void if the method does not return a value.



回答4:

In void someMethod() the generic declaration doesn't have a function, because T isn't referenced in any other part of the rest of the signature of someMethod. In this case it means nothing and can be removed.

return type is void.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!