简体   繁体   中英

Why Am I getting “Exception in thread ”main“ java.lang.StackOverflowError” in recursive java method?

I'm trying to get an output that doubles the number I enter. In my example, i put 5, and want my output to be 10, 8, 6, 4, 2. But I get an error saying

Exception in thread "main" java.lang.StackOverflowError
    at HelloWorld.recursion(HelloWorld.java:13)
    at HelloWorld.recursion(HelloWorld.java:13)
    at HelloWorld.recursion(HelloWorld.java:13)
    at HelloWorld.recursion(HelloWorld.java:13) 

However, I've seen code similar to mine and they got it correctly, what am I doing wrong? Why is line 13 wrong?

   public class HelloWorld{

     public static void main(String []args){
        System.out.println(recursion(5));
     }

     public static int recursion(int x){
         int temp = x--;
         if(x == 0){
             return 0;
         }
         else if(x > 0){
             return recursion(temp) + x*2;
         }
         return -1;
     }
}

Let's walk through each line of this when recursion(5) is called.

public static int recursion(int x){ // x is 5
     int temp = x--; // temp is 5, x is 4
     if(x == 0){
         return 0;
     }
     else if(x > 0){
         return recursion(temp) + x*2; // calls recursion(5) again
     }
     return -1;
 }

As others have mentioned above, you are using a post-decrement where you probably want a pre-decrement.

int x = 5, y = 5;
int a = x--; // a is 5, x is 4
int b = --y; // b is 4, y is 4

Change int temp = x--; to int temp = --x; Otherwise, your recursion is infinite.

public class HelloWorld{

 public static void main(String []args){
    System.out.println(recursion(5));
 }

 public static int recursion(int x){
   int recursion(int x){
     if(x == 0){
         return 0;
     }
     else if(x> 0){
          System.out.println(x*2);
          recursion(--x);
     }
 }

You are using post-decrement to assign value to int temp :

int temp = x--; is the the same as int temp = x; x--; int temp = x; x--; , so your argument for recursion in line 13 is the same as the previous function call. As a result of it, your recursion function gets into an infinite loop.

To get it to work, use pre-decrement instead, ie:

change int temp = x--; to int temp = --x; , which is equivalent to --x; int temp = x; --x; int temp = x; . Now your argument in recursion(temp) is decremented by 1.

When you call a function, its data (like arguments and local variables) is put onto what is known as the function call stack. This stack has limited space, so if your recursion does not stop you get what is known as stack overflow. Now as for the code, others have pointed out what is wrong. temp is not being assigned the value that you think it is. I would recommend adding a print statement inside the recursive function to see what values of x are actually coming in. If for example, the same value is being passed in every time then the recursion is not really making any progress and will hit the stack size limit. Hopefully that will help you narrow down similar problems in the future. Happy debugging!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM