This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compareTo 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 "compareTo" Watch "compareTo" New topic
Author

compareTo

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
source: K & B page 553


I understand what line 1 does (compareTo), but doesnt it recursively invoke compareTo itself (possible for any method?) or it is invoking some other compareTo in extending classes?


boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Not sure what you mean. That statement is calling the compareTo method of String, since title is a String reference. All classes that implement the Comparable interface have a compareTo method.

To answer your secondary question, yes, recursive method calls are fine in Java. This includes infinite recursive loops, which the compiler will accept but will cause the JVM to eventually throw a StackOverflowError.


SCJP 5.0
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy ranchers,

the invokation indicated is not recursive. It calls a method of a differnt class. To be truely recursive it has to call itself.
You can for example calculate the faculty n! of a number by using a recursive method.

Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compareTo
 
Similar Threads
Confused about use of compareTo()
how is toString method used in this question
Collections and array Problem
Comparable interface problem
Collections.sort() on Array List