Explicación de la estructura del método Java Generics

I trying to learn java generic, from aquí I got below method definition, in this can someone explain why we are declaring <T extends Comparable<T>> before the return type.

public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem)

I know the usage of Comparable interface, but why we need this <T extends Comparable<T>> before the return type of the method?

Rather we can write like public static int countGreaterThan(T[] anArray, T elem), which will also take generic parameters

So my question is Why we need <T extends Comparable<T>> o solo un <T> ¿ahí?

preguntado el 28 de mayo de 14 a las 12:05

The fact its "before the return type" is just where it happens to be. It isn't connected to the return type (in this case). It is only defining what T could be -

@Richard Tingle I think by this is way we are telling the compiler, it is generic function -

8 Respuestas

It simply means the generic type T debe extenderse Comparable<T>. By telling the compiler that, your T objects will have all the public methods of Comparable<T> a su disposición.

contestado el 28 de mayo de 14 a las 12:05

Comparable<T> is an interface whose contract means "we can compare this object to one of type T". If the object itself is of that type T, then the object is comparable to itself. This is the behavior that we desire here. Examples of similar behavior are Integer que implementa Comparable<Integer>.

Here, this method requires that you pass in objects that are comparable to themselves.

If those objects are of type T then they must implement Comparable<T> in order to be comparable with each other. Since generic extends/implements constraints use the keyword extends in all cases, the generic declaration is T extends Comparable<T>.

contestado el 28 de mayo de 14 a las 12:05

It restricts that T must be a class which implements Comparable, so that an instanceof T can compare to another instance of T

contestado el 28 de mayo de 14 a las 12:05

To be able to access: compareTo(T o); we must extend the Interfaz comparable.

Why, because only when we do that can we access it, as it isn't available beforehand.

enter image description here

Wikipedia has an excellent article on Generics in Java.

contestado el 28 de mayo de 14 a las 12:05

The method takes an array of items and a single item, and I guess from the name that it will return the number of items in anArray that are "greater than" elem.

This will work for any type T that has a well-defined "greater than" relation, i.e. any type that is Comparable to itself, which is precisely what <T extends Comparable<T>> expresses - any type T so long as that type is an implementation of Comparable with the target type itself as its type parameter.

contestado el 28 de mayo de 14 a las 12:05

I know the usage of Comparable interface, but why we need that there,or simply a <T> before the return type of the method? - Suganthan Madhavan Pillai

I know the usage of Comparable interface, but why we need that <T extends Comparable<T>> there,or simply a <T> before the return type of the method? - Suganthan Madhavan Pillai

countGreaterThan method is a generic method that requires to compare an object with another object of its type. A compareTo ( of the Comparable interface ) method provides the types implementing Comparable interface with a way to compare two objects of that type.

Since countGreaterThan method makes use of the compareTo method, it is required that the arguments supplied to the method are of those types that implement the Comparable interface.

When you declare a bounded type parameter, you're telling the compiler that T is a type that implements the Comparable interface. Hence it is safe to invoke the compareTo method on the objects of type T. So declaring the bounded type as > lets you invoke the compareTo method on the object of the type T. Now the compiler will let you invoke the compareTo method on the objects of the generic type. Not only that, the compiler will also enforce that the type of the arguments supplied to the method countGreaterThan implement the Comparable interface.

contestado el 28 de mayo de 14 a las 13:05

T means any type. So if you have the method

public static int countGreaterThan(T[] anArray, T elem)

that means you can put call it like this:

public static int countGreaterThan(String[] anArray, String elem)

or with any other class. The only constraint you have here is that both classes represented by T are the same.

The > means, that T needs to be a subclass of Comparable. So T needs to implement Comparable. An example would be Integer, which implements Comparable. That means, Integer has the method Integer.compareTo(Integer i).

The > constraint might be usefull if you want to use certain methods that are defined by that interface. For your method countGreaterThan(): this one needs to use .compareTo() to figure out if an input-object is greater than the other one, so every input-object needs to have the .compareTo()-method. So you declare > and now you can use .compareTo().

contestado el 28 de mayo de 14 a las 12:05

Since this is a static function, information about T has to accompany it. For instance methods, T will already be defined/constrained. I.e.:

class Example<T extends Comparable<T>> {
    public void doSomethingWithT(T param) { ... };
}

contestado el 28 de mayo de 14 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.