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.
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.