30

The signature of java.util.Collections.max looks like this:

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

From what I understand, it basically means that T must be both a java.lang.Object and a java.lang.Comparable<? super T>>,

However, since every java.lang.Comparable is also an java.lang.Object, what is the difference between the signature above and this below? :

public static <T extends Comparable<? super T>> T max(Collection collection);

Ben Brocka
  • 2,006
  • 4
  • 34
  • 53
Pacerier
  • 86,231
  • 106
  • 366
  • 634

1 Answers1

32

To preserve binary compatibility: It's completely described here. The second signature actually changes the return type of the method to Comparable and it loses the generality of returning an Object. The original signature preserves both.

nobeh
  • 9,784
  • 10
  • 49
  • 66
  • Thanks for the link =) Btw are you aware of any tools to examine the erased signatures after compilation? – Pacerier Apr 26 '12 at 18:55
  • Not that I know of, but maybe [this](http://blog.xebia.com/2009/03/12/a-general-purpose-utility-to-retrieve-java-generic-type-values/) can give some clues on the topic. I also suggest reading the section on "Type Erasure" at Angelika Langer's Generics FAQ. – nobeh Apr 26 '12 at 19:02
  • 2
    @Pacerier, yes, use "javap -s". – Stuart Marks Nov 29 '13 at 08:10