Image

Imageruakh wrote in Imagejava_dev

What's the difference?

The Java 5 "Generics" tutorial has a section called "Converting Legacy Code to Use Generics". It says that the JDK's Collections class has a method with the following signature:

public static <T extends Object & Comparable<? super T>>
        T max(Collection<? extends T> coll)

I understand the reason for every detail of the signature, except the part I've put in blue. The tutorial says it's because "max only reads from its input collection, and so is applicable to collections of any subtype of T". Which is true. But it doesn't seem to make any difference, since:

  • T is determined by that argument; if the argument is of type Collection<Foo>, then T is inferred to be Foo.
  • there's no way that Foo can fail the bounds-match extends Object & Comparable<? super Foo> while having a superclass that passes it.
  • I had to test this to make sure, but it still works fine without the blue part and with an argument of type Collection<? extends Foo>. (Its return type is then ? extends Foo instead of Foo, but that doesn't seem to make a difference.)

What am I missing?

I'd really appreciate something concrete to examine and play around with, such as a code fragment that either wouldn't compile without that part, or would behave sub-ideally somehow (?); but less-concrete explanations are welcome as well.

Thanks in advance!