In the wikipedia article about co- and contravariance there is an example use case and then an explanatory sentence describing what the type declaration means. I find this extremely useful. After reading the explanation a few times, I feel that I understand what it says.
<T extends Comparable<? super T>> T max(Collection<T> coll);
The bounded wildcard
? super T
conveys the information thatmax
calls only contravariant methods from the Comparable interface.
Can somebody explain in a similar language, what the type declaration on the of the andThen()
function in the java.util.function.Consumer
@FunctionalInterface
means:
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
e.g.
The bounded wildcard
? super T
conveys the information thatandThen
.... ?
And I have a secondary question: How can I find out myself, what such a type declaration means? E.g. in first example above from the java.util.Collections
util class: How are the type bounds of a class - T - able to convey information about what the methods of T are doing? Can anybody point me to the relevant paragraphs in the Java language specification for this?