Java recursion and bounded factorial
Use a base case and validate the representable range.
On this page
Static methods and conditionals.
- Use a base case and validate the representable range.
- Predict the sample output and explain the state changes.
- Complete the practice task and check the boundary cases.
The concept
Each recursive call gets its own parameter and waits for the smaller call's result. factorial(0) returns one and stops descent.
This version accepts 0 through 20 because 20! fits in long while 21! does not. Rejecting values outside the contract avoids silently overflowing the result.
Code example
class Main {
static long factorial(int n) {
if (n < 0 || n > 20) throw new IllegalArgumentException("use 0 through 20");
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println(factorial(4));
if (factorial(0) != 1) throw new AssertionError("base");
}
}24
Follow the code
Open the walkthrough to inspect the example one step at a time.
Show guided walkthrough
class Main { static long factorial(int n) { if (n < 0 || n > 20) throw new IllegalArgumentException("use 0 through 20"); if (n == 0) return 1; return n * factorial(n - 1); } public static void main(String[] args) { System.out.println(factorial(4)); if (factorial(0) != 1) throw new AssertionError("base"); }}Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Start at 4. | calls: 4 | — | |
| Descend through 3, 2, 1, and 0. | calls: 4 -> 3 -> 2 -> 1 -> 0 | — | |
| Return the base result. | factorial(0) = 1 | — | |
| Unwind with results 1, 2, 6, 24. | factorial(4) = 24 | — | |
| Print the final result. | factorial(4) = 24 | 24 |
Behind the scenes
How Java processes this code
The Java compiler produces bytecode. The JVM loads the class and invokes its main method.
Cost and common mistakes
O(n) time and O(n) stack frames for accepted n.
- A long still has a fixed maximum value.
- Recursive algorithms may exhaust stack space even when their numeric result fits; this bounded example has at most 21 calls.
Try it yourself
What happens for factorial(21)?
Reveal answer and reasoning
It throws IllegalArgumentException before attempting the multiplication.