Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

compareTo

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic