简体   繁体   中英

Java generics upper bounded wildcard restriction

Can someone explain what's the difference between these two snippets of code ?
1)

private Collection<Animal> getAnimal() {
    return null;
}

2)

private Collection<? extends Animal> getAnimal() {
    return null;
}

I understand that ? is a wildcard and I can use anything instead of it. Then I specify extends which bound that wildcard to Animal but in that case isn't the first example the same as second ? What's the difference ?
Thanks

Collection<Animal> is more restrictive than Collection<? extends Animal> Collection<? extends Animal> because Collection<Animal> matches only Animal type, but ? extends Animal ? extends Animal matches Animal or any of its subclasses. Consider below example

Example sum method will accept List<Integer> or List<Double> or List<Number>

public static double sum(List<? extends Number> numberlist) {
  double sum = 0.0;
  for (Number n : numberlist) sum += n.doubleValue();
  return sum;
 }

Main Call sum() with List<Integer> or List<Double> will work without any issues

 public static void main(String args[]) {
  List<Integer> integerList = Arrays.asList(1, 2, 3);
  System.out.println("sum = " + sum(integerList));

  List<Double> doubleList = Arrays.asList(1.2, 2.3, 3.5);
  System.out.println("sum = " + sum(doubleList));
 }

But the below method will only accept List<Number> , now if you try to call passing List<Integer> or List<double> you will have Compile time error

public static double sum(List<Number> numberlist) {
      double sum = 0.0;
      for (Number n : numberlist) sum += n.doubleValue();
      return sum;
   }

CE

The method sum(List<Number>) in the type NewMain is not applicable for the arguments (List<Double>)
The method sum(List<Number>) in the type NewMain is not applicable for the arguments (List<Integer>)

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