Java MethodsLesson 24Free preview
Java Recursion
This lesson is part of Java · All levels
Summary
Recursion is a technique where the method calls itself to break down the problem into smaller cases. Every recursion must have a base case as a stop point, otherwise the program will stop due to stack overflow. Recursion is suitable for problems like factorial and Fibonacci series.
Key points
- The recursive method calls itself into its body
- The base case must be there for the recursion to stop at some point.
- Without a base case, the program generates StackOverflowError
- Classical example: the factorial n! = n * Factorial(n-1) with Factorial(0) = 1
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.