How many times can Java recursion be performed?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.