简体   繁体   中英

Java Generics method structure explanation

I trying to learn java generic, from here 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>> or just a <T> there?

It simply means the generic type T must extend Comparable<T> . By telling the compiler that, your T objects will have all the public methods of Comparable<T> available to it.

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 which implements 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> .

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, ie 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.

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

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

在此处输入图片说明

Wikipedia has an excellent article on Generics in Java.

它限制T必须是实现Comparable的类,以便T的一个实例可以与T的另一个实例进行比较

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.

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().

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM