kzidane
kzidane

Reputation: 763

Generic Method Type Parameter

I've this method that takes 3 parameters of type T that implements interface Comparable

public static < T extends Comparable< T > > T maximum( T x, T y, T z ) {}

I am asking about declaring this in other way, like the following (assuming that it takes any Comparable object as its parameters)

public static <Comparable <T>> T maximum (T x, T y, T z){}

another question, in the first declaration, I know that Comparable is an interface so why it's written as <T extends Comparable<T>> instead of <T implements Comparable<T>>??

Upvotes: 3

Views: 221

Answers (3)

Raffaele
Raffaele

Reputation: 20875

Both of your questions can be answered with: this is the Java syntax. In the Java language a parameterized method is declared like this:

[optional modifiers] <T> [return type] foo() {}

It declares a type parameter named T, which can be constrained with an upper bound with the syntax:

T extends [type expression]

In the rest of the method (return type, formal parameters list, method body) T refers to the type you pass when invoke the method. In a declaration like:

Comparable<T>

T is not a type parameter, but it's a type argument used to instantiate the parameterized type Comparable<E>, and belongs to a bigger scope like, say:

class Foo<T> {
  public Comparable<T> foo(T arg1, T arg2) {}
}

Note that the text <Comparable<T>> (a type enclosed in angle brackets) in a source file isn't allowed at all. It doesn't mean anything to a compiler, which will refuse to compile the file.

Similarly, regarding your second question, extends is simply a keyword in the Java language and has two different meanings:

  1. It can be used in a class declaration to inherit another class
  2. It can be used to put an upper bound on a type parameter

The Java creators could have decided to use a different keyword to distinguish the two cases, but they simply overloaded an existing one because they felt it was easier to remember for developers. But they are definitely different keywords, that's why you don't use implements when declaring an upper bound for a type parameter.

Upvotes: 3

dcernahoschi
dcernahoschi

Reputation: 15230

public static <Comparable <T>> T maximum (T x, T y, T z){} is not compiling because the parameter T is undeclared for the method.

I know that Comparable is an interface so why it's written as > instead of >??

The syntax for bounded type parameters is with extends not implements even if the parameter is required to implement an interface.

Upvotes: 4

Eric Jablow
Eric Jablow

Reputation: 7889

Look at the Javadoc for Collections.sort. The signature is:

public static <T extends Comparable<? super T>> void sort(List<T> list)

This is so if I have

class A implements Comparable<A> {...}
class B extends A {...}
// ...
List<B> bs = ...
Collections.sort(bs);

I can sort the List because B is comparable through class A's comparator. So, write:

public static < T extends Comparable< ? super T > > T maximum( T x, T y, T z ) {}

Upvotes: 0

Related Questions