func
is always calling itself. There is no stopping condition. Each call creates a new stack frame, until the call stack is full and StackOverflowError
is thrown.
Recursive methods should always have a stopping condition. For example – n < 0
:
public static int func(int n){
int result;
if (n >= 0)
result = func(n - 1);
else
result = 0;
return result;
}
1
solved Why does this method cause infinite recursion? [closed]