What does it mean ...?

public static <T extends Comparable> T getMenor(T[] a) {

    if (a.length <= 1 || a == null) {
        return null;
    }

    T menorElemento = a[0];

    for (int i=1; i<a.length; i++) {

        if (menorElemento.compareTo(a[i]) > 0 ) {//Si menorEle es mayor a a[i], entonces el menor es a[i]
            menorElemento = a[i];
        }

    }

    return menorElemento;
}

I understand that the method receives a parameter of type T (GENERIC). What I don't understand is this Part:

T extends Comparable

What does it mean? Could someone explain it to me?

 7
Author: MatiEzelQ, 2016-02-24

3 answers

<T extends Comparable> it means that the method at compile time defines a generic variable type but the class or interface must implement or extend from the interface Comparable. This restricts clients ' use of this method.


It should be noted that the method has a problem: it indicates that the generic class / interface must implement / extend from Comparable but does not restrict the class to be Comparable of itself. That is, this code would be valid:

public class Bar { }
public class Foo implements Comparable<Bar> {
    @Override
    public int compareTo(Bar b) {
        //implementación descabellada, no utilizar
        //en código de producción
        return 0;
    }
}

//...

Foo[] arrayFoo = ...;
Foo menor = getMenor(arrayFoo); //compila y corre sin problemas

To fix this error, the generic should be declared like this:

<T extends Comparable<? super T>>
 7
Author: , 2016-02-24 04:09:06

Above Comparable <T> is a generic interface

public static <T extends Comparable> T ...

Now with T, it comes to be something like indicating:

it's worth you to pass me what you want but to implement the interface Comparable.


The method compareTo() that is used within that method (in your example), belongs to the interface Comparable is one of the things of why extends Comprable, so in your code you have something like this:

T menorElemento = a[0];
..//
menorElemento.compareTo()
.//

From what we deduce that the object/s T that use have to have said method, and as you get it implementing it, in this case the with Comparable Interface, and to indicate, that generic types have to implement said interface we do in the definition of the method for example:

public static <T> it indicates that it handles generic types, but so public static <T extends Comparable> we tell you that generic types have to implement the intefaz Comparable.

Those objects have to implement it, so all the parameters, for example that you let's pass getMenor(T[] a) have to have it implemented, hence the use of extends because if we pass another type that does not implement that interface, it will give you an error when doing for example this menorElemento.compareTo() and of course that T still does not have that method, I hope you understand me.

You can look at an example of the String class

public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence

Is basically something like, the T you want to use, (and the ones you return) have to have the interface that you indicate here <T extends Comparable> implemented.

 6
Author: Angel Angel, 2016-02-24 04:42:44

What T means in this context, is that it accepts any type of object that is or inherits from java.lang.Comparable (interface). In your case, Store in menorElemento the first position of the received array of type <T extends Comparable> and then perform a check of who is the least of the array.


To learn more:

  • interface java.lang.Comparable<T>: link
  • Generic Explanation: link
 1
Author: dddenis, 2016-02-24 04:32:12