Learn Java
import com.learnjava.methods.Recursion;

Recursion in Java

@Concept

A recursive method is one that calls itself, breaking a problem down into a smaller version of the same problem until it reaches a base case simple enough to answer directly without recursing further. Every recursive method needs exactly that: a base case that stops the recursion, and a recursive case that calls the method again with an input that's genuinely closer to that base case — without both pieces, a recursive method either never recurses at all or never stops.

Each recursive call gets its own separate space on the call stack to track its own parameters and local variables, which is why extremely deep recursion can exhaust that stack and throw a StackOverflowError — a real, practical limit that iterative loops don't share in the same way.

@HowItWorks
1

The base case

The simplest version of the problem, answered directly with no further recursive call.

2

The recursive case

Calls the method again with an input that's genuinely smaller or closer to the base case than the current one.

3

Every call gets its own stack frame

Each recursive call tracks its own copy of parameters and local variables, entirely separate from every other call.

4

Unwinding

Once the base case is reached, each waiting call resumes and completes in reverse order, from the deepest call back to the original one.

5

The risk of unbounded recursion

A recursive case that never actually approaches the base case recurses forever, in practice throwing a StackOverflowError once the call stack fills up.

@Example
public class RecursionDemo {
static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
 
public static void main(String[] args) {
System.out.println(factorial(4));
}
}
Output
24
1factorial(4) isn't the base case, so it calls factorial(3) and waits, planning to multiply its result by 4.
2factorial(3) similarly calls factorial(2), which calls factorial(1) — the base case, returning 1 directly with no further call.
3Each waiting call now resumes in reverse: factorial(2) computes 2 * 1 = 2, factorial(3) computes 3 * 2 = 6, and factorial(4) computes 4 * 6 = 24.
4The single println call prints that final unwound result, 24.
@Complexity
time Depends entirely on the recursive structure — factorial(n) here is O(n), since it makes exactly n calls before reaching the base case
space O(n) for the call stack — one frame per active recursive call, unwound only once the base case is reached

The stack-space cost is easy to overlook, since it doesn't show up as extra work in the code itself — it's a direct consequence of how many calls are waiting on the stack at the deepest point of recursion.

@UseCases
Solving problems that are naturally defined in terms of smaller versions of themselves, like factorial or Fibonacci
Traversing tree-shaped or nested data, like folders inside folders, where the depth isn't known ahead of time
Debugging a StackOverflowError caused by a missing or unreachable base case
Deciding when an equivalent loop would be clearer or more efficient than a recursive solution

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.