The below code snippet is from oracle docs: I have a few doubts about the syntax. Need proper and detailed explanation.
Write a generic method to find the maximal element in the range [begin, end) of a list.
public final class Algorithm {
public static <T extends Object & Comparable<? super T>>
T max(List<? extends T> list, int begin, int end) {
T maxElem = list.get(begin);
for (++begin; begin < end; ++begin)
if (maxElem.compareTo(list.get(begin)) < 0)
maxElem = list.get(begin);
return maxElem;
}
}
- Why does the method type parameter
<T extends Object & Comparable<? super T>>
T
has to extend bothObject
andComparable
. What will be the issue or difference if it were just<T extends Comparable<? super T>>
? - Why does the type parameter for
Comparable<? super T>
cannot be justComparable<T>
? - What would be the difference if the type parameter for List were just
List<T>
instead ofList<? extends T>
?