File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes compilation warnings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "compilation warnings" Watch "compilation warnings" New topic
Author

compilation warnings

AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32

hi ,
1)can anybody please explain me why it gives a warning while compilation?
2)what should be done to avoid this warning?
3)in which all conditions compilation warnings gives in generics ?

the below code is compiled in j2se 1.6 environment.

thanks in advance






AshutoshP
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
1) Warning because that is not an error but may cause problems.
2) Those warning should be removed as far as possible.
3) For generics warnings comes when you use a parametrized thing(class,interface , method) without specifying the parameter like you have done for the Comparator.

The best way to handle warnings is to use an IDE like eclipse. It will suggest you how you can remove those warnings.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Comparator is generic - it should be Comparator<ABICOMP>.

Instead of making ABICOMP implement Comparator<ABICOMP> (meaning one instance can compare any two other instances), you should consider implementing Comparable<ABICOMP> instead. This allows each ABICOMP instance to compare itself to any other instance. You will then no longer need to create an instance just to use as the Comparator. The tree set can be created as "new TreeSet<ABICOMP>()" and will let each element compare itself to the other elements.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
thanks all.

i tried with implementing implement Comparable <ABICOMP> it worked fine (no warnings).But,implementing Comparator<ABICOMP> does not work out ..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

AshutoshP Patil wrote:But,implementing Comparator<ABICOMP> does not work out ..

If you want more help with that, then please explain exactly what you mean - TellTheDetails.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
i want to sort the objects of my class ABICOMP and ptint myclass objects by toString() method
by two ways such that no warnings should be there
1)one using comparator
2)second using comparable
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 272

AshutoshP Patil wrote:thanks all.

i tried with implementing implement Comparable <ABICOMP> it worked fine (no warnings).But,implementing Comparator<ABICOMP> does not work out ..


Comparable is the right interface to use in your situation. Comparable is used on the classes that are being sorted.

Comparator is used if you want Java to ignore the rules that Comparable defines (or if the class does not implement Comparable). For example:


SCJP 6 || SCWCD 5
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
thanks michael
i got it .
 
wood burning stoves
 
subject: compilation warnings
 
Similar Threads
Sets
Generics Issue
StackOverflowError in TREESET
Comparator and TreeSet
generics...