• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about use of compareTo()

 
oli ikwayo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//On page 572 of Sierra & Bates study guide you'll find the following code:


/*
Could anyone please explain to me why this overriding of compareTo() should work.
The only thing I see is that this is an endless recursive call (we are calling compareTo()
from within compareTo()) which will crash the program.

Thanks.
*/
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DVDInfo's implementation of compareTo() calls String's implementation of compareTo(). There's nothing recursive about that.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:DVDInfo's implementation of compareTo() calls String's implementation of compareTo(). There's nothing recursive about that.


To add to that, see this link.
All Interfaces Implemented by String :
Serializable, CharSequence, Comparable<String>
 
Prabhjot Jassal
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compareTo() method needs to be written as DVDInfo class implements Comparable Interface

If the class signature looks similar to this

class DVDInfo implements Comparable {}

Then the signature of the compareTo() should be

public int compareTo(Object o) {....}

But if DVDInfo implements Comparable<DVDInfo> {}

then the signature of the compareTo() should be

public int compareTo(DVDInfo o) {...}

Answer of your question

The String class compareTo() method gets called within the DVDInfo compareTo() method

If you would have written this inside the DVDInfo compareTo() method

this.compareTo(o);
or
compareTo(o);

then it would make a recursive call..and since there is no base case, I would give you "StackOverflowError"

Hope that helps.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though equals is not implemented in this example but its worth noting that if you are implementing equals() and compareTo() then they both must be consistent to each other e..g compareTo() must return zero for two objects which are equal via equals() method.
 
oli ikwayo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your answers, particularly that of prabhjot jassal.
Strangely I was obsessed with recursion for some mysterious reason & did not see the obvious.
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
I'm confused about this as well.
the following code:


and:


what i'm confused about is the this object being passed to compareTo() which one is it?, i would like to know which title is being used to compare to DvDInfo.title?
Also is the compareTo() being called only once by Collections.sort()? what code is being used inside compareTo()?

Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic