The following describes how to cause a java. lang. StackOverflowError, that is, stack overflow:
[Java]
Public class RecursionTest
{
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
Public static void main (String [] args)
{
Int totalTimes = 1000000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}
}
Public class RecursionTest
{
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
Public static void main (String [] args)
{
Int totalTimes = 1000000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}
} Modify "totalTimes". When a certain value is reached, the following error is reported:
During development, you should avoid this problem, especially when there are too many recursive calls, it is best to replace it with for or whlie.
The following cycle () method:
[Java]
Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
}
Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
} Replace the recuresion () method:
[Java]
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
[Java]
Let's take a look at the impact on program execution efficiency:
In the preceding example, the cycle () method and the following code are added to the main () method:
[Java]
StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("--------------------
StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("-------------------- the entire code is as follows:
[Java]
Public class RecursionTest
{
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
}
Public static void main (String [] args)
{
Int totalTimes = 100000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}
}
Public class RecursionTest
{
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
}
Public static void main (String [] args)
{
Int totalTimes = 100000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}
}