This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Generics and method compareTo giving me a warning  RSS feed

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there i'm very new to learning about Generics. There is an exercise in Java How to Program by Paul and Harvey Deitel where i'm supposed to rewrite a method used in an earlier example but as a generic method. I've done it and it works fine but i keep getting this warning...

Note: GenericSelectionSort.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


So i recompile with Xlint and it say the following...


GenericSelectionSort.java:22: warning: [unchecked] unchecked call to
compareTo<T> as member of raw type java.lang.Comparable

Here is the code



So the problem is where i'm trying to use the compareTo method but i don't understand why. I mean the method makes sure that the array implements the Comparable interface. Can anyone point out what i'm doing wrong please?
 
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparable is generic as well. So your <T extends Comparable> should be <T extends Comparable<T>> or even better <T extends Comparable<? super T>>. This second form allows you to use classes like java.sql.Date, since java.sql.Date indirectly implements Comparable<java.util.Date>, not Comparable<java.sql.Date>.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Prime wrote:Comparable is generic as well. So your <T extends Comparable> should be <T extends Comparable<T>> or even better <T extends Comparable<? super T>>. This second form allows you to use classes like java.sql.Date, since java.sql.Date indirectly implements Comparable<java.util.Date>, not Comparable<java.sql.Date>.



Thank you! i can't believe i didn't spot that as i'm looking right at examples using compareTo! Oh well problem solved, thanks for your help.
 
Marshal
Posts: 61713
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not something you know until you see it written down.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!