Curriculum
Recursion in Java is a programming technique where a method calls itself repeatedly to solve a problem. Recursion is commonly used for solving mathematical problems, tree structures, searching algorithms, and complex programming tasks.
In this Core Java course in Jaipur, students will learn how recursion works in Java, recursive methods, base conditions, recursion flow, and practical examples used in software development and problem-solving.
Recursion is widely used in:
Understanding recursion in Java helps students improve logical thinking and problem-solving skills.
Recursion in Java occurs when a method calls itself directly or indirectly.
A recursive method continues execution until:
This stopping condition is called:
A recursive method mainly contains:
returnType methodName() {
if(baseCondition) {
return value;
}
return methodName();
}
class RecursionExample {
void printNumbers(int number) {
if(number <= 5) {
System.out.println(number);
printNumbers(number + 1);
}
}
public static void main(String[] args) {
RecursionExample obj = new RecursionExample();
obj.printNumbers(1);
}
}
1
2
3
4
5
Here:
printNumbers()
calls itself repeatedly.
The recursion stops when:
number > 5
Base condition prevents infinite recursion.
Without base condition:
if(number <= 5)
Every recursive method call is stored in memory stack.
Execution happens:
Factorial formula:
n!=n×(n−1)!n! = n \times (n-1)!n!=n×(n−1)!
class Factorial {
int factorial(int number) {
if(number == 1) {
return 1;
}
return number * factorial(number - 1);
}
public static void main(String[] args) {
Factorial obj = new Factorial();
System.out.println(obj.factorial(5));
}
}
120
factorial(5)
5 * factorial(4)
5 * 4 * factorial(3)
5 * 4 * 3 * factorial(2)
5 * 4 * 3 * 2 * factorial(1)
5 * 4 * 3 * 2 * 1
120
Fibonacci formula:
F(n)=F(n−1)+F(n−2)F(n) = F(n-1) + F(n-2)F(n)=F(n−1)+F(n−2)
class Fibonacci {
int fibonacci(int number) {
if(number <= 1) {
return number;
}
return fibonacci(number - 1) + fibonacci(number - 2);
}
public static void main(String[] args) {
Fibonacci obj = new Fibonacci();
for(int i = 0; i < 6; i++) {
System.out.println(obj.fibonacci(i));
}
}
}
0
1
1
2
3
5
Method calls itself directly.
Example:
display();
One method calls another method which again calls first method.
method1() → method2() → method1()
Recursion provides:
Recursion may cause:
If recursion never stops:
void display() {
display();
}
| Recursion | Loop |
|---|---|
| Method calls itself | Repeats block using loop |
| Uses stack memory | Uses iteration |
| Easier for complex problems | Faster in simple cases |
Folders inside folders use recursive logic.
Binary trees use recursion heavily.
Algorithms like binary search use recursion.
AI systems use recursive decision-making.
Factorial and Fibonacci use recursion.
Without stopping condition:
Wrong recursive logic produces incorrect output.
Too many recursive calls may reduce performance.
Understanding recursion helps students:
In this lesson, students learned:
These concepts are essential for Java programming, algorithms, and software development.
Recursion is a process where a method calls itself repeatedly.
Base condition prevents infinite recursion and stops method calls.
It occurs when recursive calls exceed memory stack size.
Recursion is used in algorithms, file systems, AI, and tree data structures.
Recursion uses self-calling methods, while loops use iteration.
Yes, recursion is very important for data structures and algorithm interviews.
WhatsApp us