• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparator interface

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's calling the compareTo method of "String b". If you look at the String class API, you'll see that it implements Comparable.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That implementation is common when the object being compared is already a Comparable. Like Christophe says, String is already a Comparable
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic