Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

java - Comparable VS <? extends Comparable>

regarding the following code:

public class Test <T extends Comparable>{
    public static void main(String[] args){
        List<String> lst = Array.asList("abc","def");
        System.out.println(func(lst));
    }
    public static boolean func(List<**here**> lst){
        return lst.get(0).compareTo(lst.get(1)) == 0;
    }
}

why writing " ? extends Comparable" here would compile , and writing "Comparable" would not compile?

thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This happens because generics are invariant. Even if String is a Comparable, meaning:

String s = "";
Comparable c = s; // would work

Generics of these would not work:

List<Comparable> listC = List.of();
List<String> listS = List.of();

listC = listS; // will fail

And this would not work no matter what is the relationship between Comparable and String.

When you change the definition of that method to:

public static boolean func(List<? extends Comparable> lst) {
    ...
}

This is said that: wildcard with an extends-bound makes the type covariant.

This means that :

List<? extends Comparable> listC = List.of();
List<String> listS = List.of();

listC = listS; // would work here

Or in simpler words it means that List<String> is a subtype of List<? extends Comparable>.

There is a small price to pay now, because listC is now a producer of elements, meaning you can take elements out of it, but you can not put anything into it.

And well after you understand this, you are not done yet, because the definition of that method would be entirely correct, when written like this:

 public static <T extends Comparable<? super T>> boolean func(List<T> lst) {
      .....
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...