File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes public static <T extends Comparable<? super T>> void sort(List<T> list) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "public static <T extends Comparable<? super T>> void sort(List<T> list)" Watch "public static <T extends Comparable<? super T>> void sort(List<T> list)" New topic
Author

public static <T extends Comparable<? super T>> void sort(List<T> list)

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
In the api for java.util.Collections i came across the following syntax :

public static <T extends Comparable<? super T>> void sort(List<T> list)


What does the above syntax mean ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's six or eight pieces of syntax there, at least. I'm sure you know what many of them are for -- I'm pretty sure you know what public is for, for example. So how about if you tell us which parts you don't understand?
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Ok fine, what does this mean ?

<T extends Comparable<? super T>>


the argument provided to the sort method should implement Comparable and not extend it?
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

jose chiramal wrote:Ok fine, what does this mean ?

<T extends Comparable<? super T>>


the argument provided to the sort method should implement Comparable and not extend it?


We cannot say
<T implements Comparable<? super T>> that is Syntax error. Even if you implement interface, you have to say <T extends Interface> . It is clearly explained in the K&B book generics section , last page or so ( i avn't remember correctly!)
The meaning is T implements Comparable interface!
Also it means all the elements in the List<T> must implement Comparable interface, or it should be mutually comparable.
All Wrapper classes, String, Date etc implements Comparable.

Anotehr example:


Hope this helps!


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
jose chiramal wrote:In the api for java.util.Collections i came across the following syntax :

public static <T extends Comparable<? super T>> void sort(List<T> list)

What does the above syntax mean ?


This declaration says, that argument to sort() method must be of a type List<T>,
where T could be any type that implements Comparable<? super T> (sort requires compareTo method defined in Comparable to compare elements of list)
Comparable<? super T> means that type ? passed to Comparable could be T or any supertype of T.

Consider this code:

Class B doesn't implement Comparable<B> (and doesn't define it's own compareTo(B b) method).
Class B inherits compareTo(A x) method from class B (we can say that it implements Comparamble<A>).
And sort(List<B>) compiles fine, it is conforming with declaration: public static <B extends Comparable<? super B>> void sort(List<B> list)
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132

Thanks, the best possible explanation one could find for this.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello All, @Ireneusz, fantastic explanation! It couldnt have been better
I have also just discovered something I didnt know -That I would have failed in the real exams- In generic type declaration the wildcard '?' and the 'super' keyword are NOT allowed, but this syntax is making that happen -though in a different way-, what I mean is this;



Will NOT compile. wow, just when you think you've learnt the necessary, new things always come up... Once again good job Ireneusz.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: public static <T extends Comparable<? super T>> void sort(List<T> list)