Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<T extends Object & E> vs <T extends E> [duplicate]

Tags:

java

generics

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);

like image 978
Pacerier Avatar asked Apr 26 '12 18:04

Pacerier


People also ask

What does Super t mean?

super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .

What does <? Extends mean in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

Does list extend Object?

There is no difference whatsoever. extends Object is redundant and assumed otherwise, just like writing class F extends Object . I would bet they are equivalent, since all classes in Java implicitly extend Object . No difference at all, it is the consequence of language design/grammar.

What does extends Object mean?

extends Object> means you can pass an Object or a sub-class that extends Object class. ArrayList<? extends Number> numberList = new ArrayList<Number>(); //Number of subclass numberList = new ArrayList<Integer>(); //Integer extends Number numberList = new ArrayList<Float>(); // Float extends Number.


1 Answers

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.

like image 97
nobeh Avatar answered Sep 24 '22 05:09

nobeh