---
title: "Recursion in java"  
description: "Recursion is a programming technique where a method calls itself repeatedly until a base condition is met."  
author: "Ashutosh Patel"  
published: 2025-03-21  
updated: 2025-03-21  
canonical: https://www.mindstick.com/articles/338832/recursion-in-java  
category: "java"  
tags: ["java", "recursion"]  
reading_time: 4 minutes  

---

# Recursion in java

[Recursion](https://answers.mindstick.com/qa/111681/what-is-recursion-and-when-should-i-use-it) is a [programming](https://www.mindstick.com/articles/65137/what-is-python-programming) technique in which a method calls itself repeatedly until a base condition is met.

Main [components](https://answers.mindstick.com/qa/96924/what-are-the-hardware-components-of-a-desktop-computer-laptop) of recursion:

- **Base case** - Stops the recursion to prevent an infinite loop.
- **Recursive case** - The function calls itself with modified parameters.

## Types of Recursion

1. Direct recursion - a method calls itself directly.
2. Indirect recursion - a method calls another method, which then calls the original method.
3. Tail recursion - the recursive call is the last statement in the function.
4. Head recursion - the recursive call occurs before any computation.
5. Tree recursion - a function makes multiple recursive calls.
6. Nested recursion - the argument to a [recursive function](https://www.mindstick.com/forum/156808/write-a-program-for-making-recursive-function-in-any-language) is itself a recursive call.

## 1. Direct Recursion

Let's [calculate the factorial](https://www.mindstick.com/forum/158751/create-a-function-to-calculate-the-factorial-of-a-given-number-using-recursion) of a number using **Direct** recursion:

```java
class RecursionExample {
   static int factorial(int n) {
       if (n == 0 || n == 1)  // Base Case
           return 1;
       return n * factorial(n - 1); // Recursive Case
   }
   public static void main(String[] args) {
       System.out.println("Factorial of 6: " + factorial(6));
   }
}
```

## Output

```plaintext
Factorial of 6: 720
```

## 2. Indirect Recursion

In indirect recursion, two or more functions call each other.

```java
class IndirectRecursion {
   static void methodA(int n) {
       if (n > 0) {
           System.out.print(n + " ");
           methodB(n - 1);
       }
   }
   static void methodB(int n) {
       if (n > 0) {
           System.out.print(n + " ");
           methodA(n / 2);
       }
   }
   public static void main(String[] args) {
       methodA(10);
   }
}
```

## Output

```plaintext
10 9 4 3 1
```

## 3. Tail Recursion

In tail recursion, the recursive call is the last [operation](https://answers.mindstick.com/qa/97910/what-is-the-aim-of-operation-namaste) in the function.

```java
class TailRecursionExample {
   static void printNumbers(int n) {
       if (n == 0) return; // Base Case
       System.out.println(n);
       printNumbers(n - 1); // Tail Recursion
   }
   public static void main(String[] args) {
       printNumbers(5);
   }
}
```

Optimized for performance, as there is no additional computation after the recursive call.

## Output

```plaintext
5
4
3
2
1
```

## 4. Head Recursion

In head recursion, the recursive call is made before any computation is performed.

```java
class HeadRecursionExample {
   static void printNumbers(int n) {
       if (n == 0) return; // Base Case
       printNumbers(n - 1); // Head Recursion
       System.out.println(n);
   }
   public static void main(String[] args) {
       printNumbers(5);
   }
}
```

## Output

```plaintext
1
2
3
4
5
```

## 4. Tree Recursion

In tree recursion, a function calls itself multiple times.

```java
class TreeRecursionExample {
   static void treeRec(int n) {
       if (n == 0) return;
       System.out.println(n);
       treeRec(n - 1);
       treeRec(n - 1);
   }
   public static void main(String[] args) {
       treeRec(3);
   }
}
```

## Output

```plaintext
3
2
1
1
2
1
1
```

## 5. Nested Recursion

In nested recursion, the argument of the function is itself a recursive call.

```java
class NestedRecursionExample {
    static int nestedRec(int n) {
        if (n > 100) return n - 10;
        return nestedRec(nestedRec(n + 11));
    }

    public static void main(String[] args) {
        System.out.println(nestedRec(95));
    }
 }
```

Nested recursion is rare but useful in complex mathematical calculations.

## Output

```plaintext
91
```

## Recursion vs. Iteration

| **Feature** | **Recursion** | **Iteration** |
| --- | --- | --- |
| [Definition](https://yourviews.mindstick.com/view/70598/false-furore-over-the-leadership-definition-statement-of-army-chief-bipin-rawat) | Function calls itself | Uses loops (`for`, `while`) |
| [Memory Usage](https://www.mindstick.com/forum/158210/what-are-some-common-techniques-for-optimizing-memory-usage-in-computer-systems) | Uses more memory (stack frames) | Uses less memory |
| Speed | Can be slower (due to stack overhead) | Faster in many cases |
| Complexity | Simpler for problems like [tree traversal](https://www.mindstick.com/forum/159507/handle-null-checks-in-tree-traversal) | More efficient in loops |
| Example | Fibonacci, Factorial, Tower of Hanoi | Printing numbers, summation |

## When to use recursion?

- When the problem is naturally broken down into smaller sub-problems.
- In tree structures, graphs, and divide-and-conquer algorithms.
- When code readability is more important than performance.

Also, read: [Pass by Value vs. Pass by Reference in Java](https://www.mindstick.com/articles/338817/pass-by-value-vs-pass-by-reference-in-java)

---

Original Source: https://www.mindstick.com/articles/338832/recursion-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
