Reputation: 2963
I am trying to understand this piece of Code. It is from Oracle Generics Page.
I am seeing two return types here <T extends Comparable<T>>
and int
. Am I reading this right ? If so how can a method have two return types ?
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e.compareTo(elem) > 0)
++count;
return count;
}
Upvotes: 1
Views: 96
Reputation: 1879
The method has only one return type: int.
<T extends Comparable<T>>
is just to declare the type of T.
If you do something like this:
public static int calculate(T param) {
....
}
You would have a compilation error as T is undefined. T is a generic type so you need to specify it:
public static <T> int calculate(T param) {
....
}
Upvotes: 1
Reputation: 28752
No, the return type is int
T extends Comparable<T>
is type parameter, and used in the parameters.
Upvotes: 3