Recursion solves a big problem by calling itself on a smaller one — until the trivial case answers directly.
1 A method calls itself
factorial(n) is defined as n * factorial(n - 1): each call works on a smaller copy of the same problem.
2 Base case
The stop condition — the smallest input answered directly. Without it, the calls never end.
3 Recursive case
The method calls itself with input one step closer to the base case, like n - 1.
4 The stack remembers
Every pending call waits in its stack frame until the innermost call returns.
Shrink the problem
A method calls itself1factorial(n) is defined as n * factorial(n - 1): each call works on a smaller copy of the same problem. Base case2The stop condition — the smallest input answered directly. Without it, the calls never end.
Recursive case3The method calls itself with input one step closer to the base case, like n - 1. The stack remembers4Every pending call waits in its stack frame until the innermost call returns. 4 building blocks make up Shrink the problem
Play the script: frames pile up as factorial calls itself, then pop off as the answers come back.
Drag to orbit the scene. Press Play to watch memory in action.
Press play: three frames pile up on the way down, then the answers unwind 1 → 2 → 6.
JVMThe JVM loads FactDemo — one main plus one recursive helper.
Memory
no variables yet — press play
Console
output appears as the program runs…
A recursive countdown with a step of 2 — trace every call before you guess.
Your predicted output — one line per console line
Hint: Each call prints n, then calls itself with n - 2. When n reaches 0 it prints Go! and stops.
Click a term, then click its meaning to lock the pair.
5 conceptsTap a card to expand ?base casedrops into place
?recursive casedrops into place
?stack framedrops into place
?StackOverflowErrordrops into place
?unwindingdrops into place
01Every recursion needs a base case — the smallest input answered directly.
02The recursive case must move toward the base case (n - 1, n - 2, ...).
03Each call waits on the stack until the call above it returns.
04factorial(n) = n * factorial(n - 1): shrink on the way down, multiply on the way back.
05No base case? Frames pile up until StackOverflowError.