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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused about use of compareTo()" Watch "Confused about use of compareTo()" New topic
Author

Confused about use of compareTo()

oli ikwayo
Greenhorn

Joined: Jun 15, 2011
Posts: 11
//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
Bartender

Joined: Oct 14, 2005
Posts: 18710
    
    8

DVDInfo's implementation of compareTo() calls String's implementation of compareTo(). There's nothing recursive about that.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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>

SCJP 6. Learning more now.
Prabhjot Jassal
Greenhorn

Joined: Jul 19, 2010
Posts: 22
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

Joined: Oct 15, 2010
Posts: 281

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.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
oli ikwayo
Greenhorn

Joined: Jun 15, 2011
Posts: 11
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

Joined: Apr 25, 2010
Posts: 71
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about use of compareTo()