• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, the best possible explanation one could find for this.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic