In this tutorial, you will learn about Java recursion with examples, its advantages, and disadvantages.

**Recursion is the process of a function calling itself directly or indirectly**, and the corresponding function is known as a recursive function. Certain problems can be solved quickly using the recursive algorithm. Towers of Hanoi (TOH), Tree Traversals, DFS of Graph, and others are examples of such problems.

A recursive method is a method that calls itself in Java. Recursion is the term for this process. |

**for example**: Place two parallel mirrors facing each other in the physical world as an example. Any object between them would be recursively reflected.

Contents

## How Recursion work?

public static void main(String[] args){ //... recurse() } static void recurse(){ //.... recurse() }

In the preceding example, we called the recurse() method from within the main method. (normal method call) And, within the recurse() method, we’re calling the same recurse method yet again. This is a recursive function call.

**Note**: To stop the recursive call, we must include some conditions within the method. If not, the method will be called indefinitely.

## Recursion Example: Factorial of a Number

class Factorial { static int factorial( int n ) { if (n != 0) // termination condition return n * factorial(n-1); // recursive call else return 1; } public static void main(String[] args) { int number = 5, result; result = factorial(number); System.out.println(number + " factorial = " + result); } }

**Output:**

5 factorial 120 |

In the preceding example, we have a method known as factorial (). The factorial() method is called the main() method. using the number variable as an argument

Take note of the statement:

return n * factorial(n-1); |

- The factorial() method is called the factorial() method. To begin, the value of n is 5 inside factorial (). 4 is passed to the factorial() method during the next recursive call. This process is repeated until n equals 0.
- When n equals 0, the if statement returns false, and thus 1 is returned. The accumulated result is then passed to the main() method.

## Pros and Cons of Recursion

- When a recursive call is made, new variable storage locations are allocated on the stack. The old variables and parameters are removed from the stack as each recursive call returns. As a result, recursion generally consumes more memory and is slower.
- A recursive solution, on the other hand, is much simpler to write, debug, and maintain.

## Example: Java Recursion

### A Basic Java recursion example

public class Basic { public static void main(String[] args) { itSelf(5); } /* Recursive Java method */ public static void itSelf(long i) { if (i < 0) { return; } System.out.print(i); i = i - 1; itSelf(i); } }

**Note: **This program simply passes the number 5 to the itSelf method of the program. This method prints the number subtracts one from it and then repeats until the number zero is reached. As a result, In reverse order, all of the numbers from the given number to one are printed.

### Add a series of numbers

public class AddSeries { public static void main(String[] args) { long Add = Add(5); System.out.println(Add); } /* A recursive Java example to sum all natural numbers up to a given long. */ public static long Add(long number) { /* Stop the recursive Java program at zero */ if (number != 0) { return number + Add(number - 1); } else { return number; } } }

**Note**: The recursive Java method in the previous example returned void. The recursive method in this example returns a whole number that represents an ongoing sum.

- The following is an example of recursive Java logic. Begin with a number and then multiply it by one less than itself. That logic should be repeated until you reach zero. When zero is reached, the total sum of all numbers from the starting number down to zero is computed.

### Recursive Fibonacci series

public class FibonacciSeries{ public static void main (String args[]) { for(long i=1; i<=5; i++){ System.out.print(fibonacciSeries(i) +" "); } System.out.println(); } /* A recursive Fibonnaci sequence in Java */ public static long fibonacciSeries(long number) { if (number == 1 || number == 2) { return 1; } return fibonacciSeries(number - 1) + fibonacciSeries(number - 2); } }

**Note: **The Fibonacci series will be calculated in both iterative and recursive Java programs in this example. When it comes to learning recursion, this is the most common assignment given to new developers. Here’s what it looks like when done in a purely recursive manner.

### Recursive Java palindrome program

public class Palindrome{ public static void main(String[] args) { boolean get = palindromeProgram("siteaspsaetis"); System.out.println(get); get = palindromeProgram("five"); System.out.println(get); get = palindromeProgram("spinnerennips"); System.out.println(get); } /* Recursive Java example to check for palindromes */ public static boolean palindromeProgram(String s){ if(s.length() == 0 || s.length() == 1) { return true; } if(s.charAt(0) == s.charAt(s.length()-1)) { return palindromeProgram(s.substring(1, s.length()-1)); } return false; } }

**Note: **In this Java recursion example, we first see if the first and last letters of a String are the same. We then move one letter in from both the beginning and end of the String and perform the same String comparison recursively. If all of the checks return true, our Java palindrome program will also return true. If this is not the case, the palindrome checking program returns false.

recursion java,recursion java recursion in java, recursion in java ,

**You may like:**

Java this Keyword with Example

Hope this article will guide you to recognize all about the **Java Recursion with Example** that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.