Recursion is one of the tough programming technique to master. Many programmers working on both Java and other programming languages like C or C++ struggles to think recursively and figure out the recursive pattern in the problem statement, which makes it is one of the favorite topics of any programming interview. If you are new in Java or just started learning Java programming language and you are looking for some exercise to learn the concept of recursion than this tutorial is for you. In this programming tutorial, we will see a couple of example of recursion in Java programs and some programming exercise which will help you to write recursive code in Java e.g. calculating Factorial, reversing String and printing Fibonacci series using recursion technique. For those who are not familiar with recursion programming technique here is the short introduction: "Recursion is a programming technique on which a method call itself to calculate result". It's not as simple as it look and mainly depends upon your ability to think recursively. One of the common trait of recursive problem is that they repeat itself, if you can break a big problem into small junk of repetitive steps then you are on your way to solve it using recursion.
How to solve problem using Recursion in Java
In order to solve a problem using recursion in Java or any other programming language e.g. C or C++, You must be able to figure out :
1) Base case, last point which can be resolve without calling recursive function e.g. in case of Fibonacci series its
1 and 2 where result will be 1. In the case of a recursive power function, it's zero power which is equal to 1 or in the case of calculating Factorial its factorial of zero which is equal to 1.
2) With every recursive method call, Your problem must reduce and approach to the base case. If this is not the case then you won't be able to calculate the result and eventually die with java.lang.StackOverFlowError
Recursion Programming Example in Java
In our programming example of recursion in Java, we will calculate Fibonacci number of giving length using recursion. In the case of Fibonacci number current number is the sum of previous two number except first and second number which will form the base
case for the recursive solution.
If you look above example of recursion in Java you will find that we have a base case where program returns a result before calling recursive function and then with every invocation, the number is decreased by 1. This is very important to reach a solution using the recursive technique.
Programming Exercise to solve using Recursion
Here are few more programming exercise to learn Recursion programming technique in Java programming language. This exercise are solely for practicing. In order to understand Recursion properly you must try to think recursive e.g. look tree as collection of small tree, look string as collecting of small String, look staircases as collection of small staircase etc. Any way try to solve following programming exercise by using Recursion programming technique for better understanding
1. Print Fibonacci series in Java for a given number, see here for solution
2. Calculate factorial of a given number in Java, sees here for solution of this programming exercise
3. Calculate power of a given number in java
4. Reverse a String using recursion in Java, see here for solution
5. Find out if there is a loop in linked list using recursion
This was a simple introduction of Recursion programming technique to Java programmer with most basic examples. There are a lot more to learn on Recursion including different types of recursion e.g. tail recursion, improving the performance of recursive algorithm using memoization or caching pre-calculated result etc. Its also recommended not to use the recursive method in production code instead write iterative code to avoid any StackOverflow error.
Other programming tutorials from Javarevisited Blog