codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

23 / 65 topics
19Java Methods20Java Method Parameters21Java Method Overloading22Java Scope23Java Recursion
Tutorials/Java Programming/Java Recursion
☕Java Programming

Java Recursion

Updated 2026-05-12
30 min read

Java Recursion

Recursion is a powerful programming technique where a method calls itself to solve smaller instances of the same problem. This approach is particularly useful for problems that can be broken down into simpler, repetitive tasks. Understanding recursion is essential for writing efficient and elegant code, especially in areas like algorithms and data structures.

In this tutorial, we'll explore what recursion is, how it works, and how to implement it correctly in Java. We'll cover the concept of a base case, which is crucial for preventing infinite loops, and provide several examples to illustrate these concepts.

What Is Recursion?

Recursion occurs when a method calls itself directly or indirectly. Each recursive call should work on a smaller instance of the same problem until reaching a base case, which stops further recursion. This process can be visualized as a stack where each function call is added to the top and removed from the top once completed.

Real-World Analogy

Imagine you have a set of Russian nesting dolls. To open the largest doll, you need to open the next smaller one inside it, and so on, until you reach the smallest doll. This process of opening each smaller doll is similar to recursion: each step involves solving a smaller version of the same problem.

Core Concepts

Base Case

The base case is the condition under which the recursion stops. Without a proper base case, the method would keep calling itself indefinitely, leading to a stack overflow error. The base case should be simple enough to resolve without further recursive calls.

Halting Condition

The halting condition is another term for the base case. It ensures that the recursion terminates and prevents infinite loops. A well-defined halting condition is essential for the correct functioning of any recursive method.

Examples

Let's look at some examples to understand how recursion works in Java.

Example 1: Factorial Calculation

The factorial of a non-negative integer \( n \) is the product of all positive integers less than or equal to \( n \). It can be defined recursively as:

\[ n! = n \times (n-1)! \]

with the base case being \( 0! = 1 \).

Factorial.java
1public class Factorial {
2 public static int factorial(int n) {
3 if (n == 0) { // Base case
4 return 1;
5 }; else {
6 return n * factorial(n - 1); // Recursive call
7 }
8 }
9
10 public static void main(String[] args) {
11 System.out.println("Factorial of 5 is: " + factorial(5));
12 }
13}
Output
Factorial of 5 is: 120

Example 2: Fibonacci Sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. It can be defined recursively as:

\[ F(n) = F(n-1) + F(n-2) \]

with the base cases being \( F(0) = 0 \) and \( F(1) = 1 \).

Fibonacci.java
1public class Fibonacci {
2 public static int fibonacci(int n) {
3 if (n == 0) { // Base case
4 return 0;
5 } else if (n == 1) { // Base case
6 return 1;
7 } else {
8 return fibonacci(n - 1) + fibonacci(n - 2); // Recursive call
9 }
10 }
11
12 public static void main(String[] args) {
13 System.out.println("Fibonacci of 5 is: " + fibonacci(5));
14 }
15}
Output
Fibonacci of 5 is: 5

Example 3: Sum of Digits

The sum of the digits of a number can be calculated recursively by adding the last digit to the sum of the remaining digits.

SumOfDigits.java
1public class SumOfDigits {
2 public static int sumOfDigits(int n) {
3 if (n == 0) { // Base case
4 return 0;
5 } else {
6 return n % 10 + sumOfDigits(n / 10); // Recursive call
7 }
8 }
9
10 public static void main(String[] args) {
11 System.out.println("Sum of digits in 12345 is: " + sumOfDigits(12345));
12 }
13}
Output
Sum of digits in 12345 is: 15

Practical Example

Let's create a practical example that demonstrates the use of recursion to reverse a string.

Java
1public class ReverseString {
2 public static String reverse(String str) {
3 if (str.isEmpty()) { // Base case
4 return str;
5 } else {
6 return reverse(str.substring(1)) + str.charAt(0); // Recursive call
7 }
8 }
9
10 public static void main(String[] args) {
11 System.out.println("Reversed string: " + reverse("Hello"));
12 }
13}
Output
Reversed string: olleH

Summary

  • Recursion is a method that calls itself to solve smaller instances of the same problem.
  • A base case or halting condition is essential to prevent infinite loops and ensure termination.
  • Recursive methods should be designed with simple base cases and clear recursive steps.

What's Next?

Now that you have a solid understanding of recursion, it's time to explore object-oriented programming (OOP) in Java. OOP provides a way to structure programs using classes and objects, which can help manage complexity and promote code reuse. In the next tutorial, we'll dive into the fundamentals of OOP, including encapsulation, inheritance, and polymorphism.

Stay tuned for more advanced topics and continue your journey in Java programming!


PreviousJava ScopeNext Java OOP

Recommended Gear

Java ScopeJava OOP