Junior — Middle
What is the term for a situation where a method calls itself?
sobes.tech AI
Answer from AI
A situation where a method calls itself within its body is called recursion.
A recursive method usually has a base case that stops further calls, and a recursive case that calls the method again with modified parameters.
Example of recursion in Java:
public int factorial(int n) {
if (n <= 1) return 1; // base case
else return n * factorial(n - 1); // recursive call
}
Here, the factorial method calculates the factorial of a number by calling itself with a reduced argument.