File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Bonus Exam, Comparable doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Bonus Exam, Comparable doubt" Watch "K&B Bonus Exam, Comparable doubt" New topic

K&B Bonus Exam, Comparable doubt

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710

You can use java.lang.Comaprable and java.util.Comparator to
sort collections whole elements are of any valid java type,
as long as all of the collection's elements are of the same class.

This is declared as wrong option.
The reason is given below:

It is incorrect because if a class is final and does not alreadt
implements Comparable, then Comparable can't be used to sort that

I don't understand what is that talking about.

Please guide.


megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi Chandra,

Its sort elements of a class using Comparable, the implementation of the class has to be modified to include CompareTo() method.

So if the class doesnt implement Comparable and its final....than we can't add CompareTo() to it right...

So it can only be sorted using Comparator can be implemented by a external class rather than the class itself.

As the statement uses the word "and" its false...if it would have used "or" it will be true.

(I know I gave such an answer before about a statement using word "creates" )I might not be right here tooo....
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
Thanks Megha,

I got that!
I agree. Here's the link:
subject: K&B Bonus Exam, Comparable doubt
It's not a secret anymore!