Solution for StackOverFlowError | Infinite Recursion | Java

StackOverFlowError is a common Javascript Error. A common reason why a StackOverflowError being thrown is when a call stack exceeds due to excessive deep or infinite recursion. The simplest solution for the StackOverflowError is to detect the repeating pattern and terminate and close off the lines properly. Recursions can be confused with loops however a recursion is a technique in which function call its self again and again until the user terminates the condition.

What is StackOverFlowError?

Missing base criteria (terminating condition)causes a Stack OverFlow Error in java while doing recursion .It become infinite recursion like infinite loop which never end.

Message

Error Type:

StackOverFlowError

 

What is Recursion?

Recursion is a technique in which function call its self again and again until terminating condition occur.Terminating condition is basically a base criteria which is used to break the recursive call of function.

When there are too many function call or terminating condition(base criteria) is missing java throw an error StackOverFlowError.

For Example

This recursion function run 20 times ,as per exit condition( x>=20)

static void recursionFunction(int x)
{
if(x>=20)
return;

recursionFunction(x+1);//recursive call }

If the value set to extremely high in base criteria ,it won’t work.

 For example

static void recursionFunction(int x)
{
if(x>=100000000000)
return;
recursionFunction(x+1);//recursive call
}

Error:This Extremely high value is out of range.

If recursive function is missing the base criteria (exit condition) ,then function perform infinite recursion.

For example

static void recursionFunction(int x)
{
//No base criteria is define so it will generate infinite recursion
recursionFunction(x+1);
}

It will generate StackOverFlowError.

Output:

Solutions-for-Stack-OverFlow-Error-infinite-recursion-java

To avoid this problem it is important to define the base criteria(with limited value) in recursion function.

 

Java Infinite Loop vs. Infinite Recursion

Basically both use to execute the instruction repeatably ,but there is major difference between two. In recursion method call itself again and again however in a loop, both required the condition to execute repeatably, but there is some another key differences that makes it clear how they are different programming concept.

  1. In recursion code is smaller as compared to loop.
  2. Infinite recursion may be the cause of system crash but infinite loop consumes the cycles of processor.
  3. In recursion base criteria decide the number of iterations but in loop variable values decide how many time the loop execute.

Let understand this concept with an example

How recursions work?

public static void main(String[] args)
{
int f= factorial(5);
System.out.println("factorial of 5 is:"+f);
}
static int factorial(int x)
{
if(x<=1)
return 1;
else
return x*factorial(x-1);
}

In this example we can see function Factorial recursive call and print the factorial of 5.

How Loops work?

public static void main(String[] args)
{
factorial(5);
}
static void factorial(int x)
{
for(int i=4;i>=1;i--)
{
x=x*i;
}
System.out.println("factorial of 5 is:"+x);
}

In this example we can see that the function will end when it meets an acceptance criteria.

Solution for StackOverFlowError

The simplest solution for the StackOverflowError is:

  1. Go back to the source code
  2. Detect the repeating pattern
  3. Terminate and close off the code properly so it removes the recursion that is occurring in the code

 

For more fixes on common Java Errors, check out our articles below:

 

Comments are closed.