aspose file tools*
The moose likes Beginning Java and the fly likes Generics and method compareTo giving me a warning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics and method compareTo giving me a warning" Watch "Generics and method compareTo giving me a warning" New topic
Author

Generics and method compareTo giving me a warning

Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
It's not something you know until you see it written down.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics and method compareTo giving me a warning