wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparator interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparator interface" Watch "Comparator interface" New topic
Author

Comparator interface

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

This is from KS and BB book, why it is getting possible for the class ReSortComparator to use compareTo() method of Comparable interface in compare() method, though ReSortComparator class doesn't implements it.
And whats actually happening at line 12 and why we have done so..?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

It's calling the compareTo method of "String b". If you look at the String class API, you'll see that it implements Comparable.


[My Blog]
All roads lead to JavaRanch
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

That implementation is common when the object being compared is already a Comparable. Like Christophe says, String is already a Comparable


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

Christophe Verré and Deepak Bala thanks for your replies !!

1> I got from here that the Strings objects were compared. And as the String class already implements the Comparable interface, we can use the compareTo() method over its objects directly. Am i right... ?

2> What will happen internally when line #12 will be executed, I mean to say how the control will flow and which line will do what until the call returns?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1> Yes.

2> The API of the sort method will give you an hint on how things are done. For more internal details, you can still look at the source of the Arrays class.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

swaraj gupta wrote:Christophe Verré and Deepak Bala thanks for your replies !!

1> I got from here that the Strings objects were compared. And as the String class already implements the Comparable interface, we can use the compareTo() method over its objects directly. Am i right... ?



Yep

swaraj gupta wrote:

2> What will happen internally when line #12 will be executed, I mean to say how the control will flow and which line will do what until the call returns?


The code flow would depend on Arrays.sort()'s internal implementation. Your ResortComparator will be called at sometime during the sorting process. The how, when, why of it is transparent to the caller.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Comparator interface
 
Similar Threads
Object of static class
Comparator interface
Searching Arrays and Collections
Insertion Point Question on binarySearch()
Comparator's compare() method question