Question

For example, I see

static <T extends Comparable<? super T>> void parallelSort(T[] a)

Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.

So I get this is a static method and what it does, but what does <T extends Comparable<? super T>> mean (return type, but what is it, is it the same way I would write in the code, or a syntax used in the documentation to show several possible values)

Was it helpful?

Solution

<T extends Comparable<? super T>> is not the return type. void is the return type. A generic specification before the return type is for type inference.

The argument (T[]) type is inferred from the call, and must be something that extends Comparable<? super T>

Suppose you have a class defined:

class Foo extends Comparable<Foo> { ... }

That means:

Foo[] fooArray = ...
parallelSort(fooArray);

is legal, and inside the parallelSort() method, T will be of type Foo (which implements the Comparable interface)

Here's a simple, less complicated example without the recursive type. In this case, it says it returns a list of the inferred type:

public static <T> <List<T>> myMethod(T arg) {
    List<T> list = new ArrayList<T>();
    list.add(arg);
    return list;
}

The type is inferred from the argument:

List<String> list = myMethod("hi");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top