Introduction to Recursion In Java
A method that calls itself is known as a recursive method. And, this process is known as recursion. In order to stop the recursive call, we need to provide some conditions inside the method. Otherwise, the method will be called infinitely.
-
Infinite Recursion
static void infiniteRecursion() { System.out.println("This is a infinite recursion!"); infiniteRecursion(); }
-
Finite Recursion
static void finiteRecursion(int x) { x++; if(x <= 10){ System.out.println("This is a finite recursion!"+x); finiteRecursion(x); } }
-
Base Condition/ Halt Condition (i.e., when to stop)
-
Work toward Base Condition
-
Recursive Call.
- Summing list of numbers
- Factorial of a number
- Compute the Nth number in the Fibonacci sequence
- Sorting
Some recursive algorithms are very similar to loops. These algorithms are called "tail recursive" because the last statement in the algorithm is to "restart" the algorithm. Tail recursive algorithms can be directly translated into loops.
static int sumOfNumbers(int... args){
if (args.length == 0)
return 0;
else
return args[args.length-1] + sumOfNumbers(Arrays.copyOf(args, args.length-1));
}
When a recursive call is made, new storage locations for variables are allocated on the stack. As, each recursive call returns, the old variables and parameters are removed from the stack. So, recursion generally uses more memory and is generally slow.
On the other hand, a recursive solution is much simpler and takes less time to write, debug and maintain.