Skip to content

Commit 6f7ea86

Browse files
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

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

Section10Methods/RecurssionPractice/src/RecursionIsCool.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
public class RecursionIsCool {
22
public static void main(String[] args) {
3-
//calling a method sayHi.
3+
//calling a method sayHi. Method called it-self.
44
sayHi();
5-
//Method called it-self.
65
}
76
public static void sayHi() {
87
System.out.println("Hi");

0 commit comments

Comments
 (0)