• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Confused about use of compareTo()  RSS feed

 
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.
*/
 
Marshal
Posts: 24195
54
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.
 
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>
 
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.
 
Ranch Hand
Posts: 300
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.
 
Ranch Hand
Posts: 71
  • Likes 1
  • 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.
 
Ranch Hand
Posts: 30
  • 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.



How are we able to use String class methods in another class? I know all classes inherit Object class so we are able to use the Object class methods Object.equals(anotherObject) in any class but how come we can use String class methods?
 
Greenhorn
Posts: 25
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can use String methods in another class: On objects of type String.

The compareTo method in DVDInfo returns...

...where both "title" and "d.getTitle()" are strings, so you call the compareTo method of the String class here.


Mike
 
Karla Carr
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Stiefler wrote:Of course you can use String methods in another class: On objects of type String.

The compareTo method in DVDInfo returns...

...where both "title" and "d.getTitle()" are strings, so you call the compareTo method of the String class here.


Mike



If I have class A and class B then can I directly use the method of class A in class B? I would have to instantiate the class A in class B to use it. So, how do I directly use String class methods directly everywhere?
 
Marshal
Posts: 63461
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful about saying, “class methods”; that usually means static methods. You probably mean to use instance methods.
You can use methods of a String object anywhere you have a String object. Just as you can use the methods of any object anywhere you can find such an object, subject to the usual rules about access modifiers.If you can get that sort of code to compile at all, you can call methods of a String like that. Line 5 shows that a String literal is an object in its own right.
 
Karla Carr
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Careful about saying, “class methods”; that usually means static methods. You probably mean to use instance methods.
You can use methods of a String object anywhere you have a String object. Just as you can use the methods of any object anywhere you can find such an object, subject to the usual rules about access modifiers.If you can get that sort of code to compile at all, you can call methods of a String like that. Line 5 shows that a String literal is an object in its own right.



Thank you! I did figure it out by myself when I actually gave it a good thought but that really clarifies things well!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!