Previously, I tried to solve the problem of jiugong Ge using recursive methods, prompting stack overflow. I'm curious about how many levels of recursion can Java be implemented, and what factors are related to it?
Today I did a boring test.
I wrote a method.
Public void rec1num (int I)
{
System. Out. println ("this is" + I + "recursion ");
Rec1num (I + 1 );
}
Then call. The result is recursive for 5093 times and an error is returned. (It may not always be recursive so many times, but it is about this value)
What if I add a new number?
Write another two numbers. It runs 3764 times.
Public void rec2num (INT I1, int I2)
{
Int I;
I = (I1 + I2 + I1 + I2)/4;
System. Out. println ("this is" + I + "recursion ");
Rec2num (I1 + 1, I2 + 1 );
}
I think what if I add more things in recursion. Two useless for Loops
Public void rec2num2 (INT I1, int I2)
{
Int I;
For (I = 0; I <1000; I ++)
I1 ++;
For (I = 0; I <1000; I ++)
I1 --;
System. Out. println ("this is" + I1 + "recursion ");
Rec2num2 (I1 + 1, I2 + 1 );
}
It runs 3141 times.
It seems that the number of recursive calls and the number of parameters are related to the complexity of recursive functions. Is it related to machines? Try another computer at night.