Commit 6f7ea86
committed
feat: Add RecursionIsCool class demonstrating infinite recursion
WHAT the code does:
- Defines a `RecursionIsCool` class.
- `sayHi()` method:
- Prints `"Hi"`.
- Immediately calls itself again without a base case.
- `main()` starts recursion by calling `sayHi()`.
WHY this matters:
- Demonstrates **infinite recursion** (a method calling itself without a stopping condition).
- Useful as an example of what **not to do** in recursion.
- Highlights importance of base cases to prevent stack overflow.
HOW it works:
1. `sayHi()` prints `"Hi"`.
2. Calls itself again, repeating indefinitely.
3. Eventually causes a `StackOverflowError` because Java call stack overflows.
Tips & gotchas:
- Always ensure recursion has a **base case** to terminate safely.
- Infinite recursion can freeze or crash programs.
- This pattern is fine for demonstration but dangerous in real applications.
- To fix: add a condition (e.g., limit number of recursive calls).
Use-cases:
- Teaching what happens with **missing base cases**.
- Demonstrating stack overflow errors in Java.
- Educational tool for recursion debugging.
Short key: demo-infinite-recursion-stackoverflow.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 0366936 commit 6f7ea86
1 file changed
+1
-2
lines changedLines changed: 1 addition & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
0 commit comments