Recursive bound Comparable<T>: one max for every comparable type
Version 1.10 beats 1.9 because compareTo compares numbers, not text. As strings, "1.10" would sort before "1.9".
A bound limits which types a type parameter accepts: <T extends Number> means "any Number or subclass", and <T extends Comparable<T>> means "any type that can compare itself". In return, your generic code may call the bound's methods, like doubleValue() or compareTo(), on values of type T.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write static <T extends Comparable<? super T>> T min(List<T> list) and test it with integers and strings.
Write a class Range<T extends Comparable<? super T>> with contains(T value) and test it with Range<Integer>(1, 10) and Range<String>("b", "m").
Write static <T extends Number> double[] toDoubles(List<T> list) and print the result for List.of(1, 2, 3) with Arrays.toString.
Explain it without notes
What does an upper bound like <T extends Number> do for the caller and for the code inside the method?
Explain <T extends Comparable<T>>. Why is it called a recursive bound, and what problem does Comparable<? super T> solve?
What are the rules for multiple bounds, and why does the order matter?
How does a bound affect the bytecode of a generic method?
Expected output
17
pear
1.10
true false