简体   繁体   中英

Strange Java Multiplication Recursion with StackOverFlow

The Java program functions correctly using iteration and recursion. But for some strange reason I can't understand is that when the numbers I enter with anything above 9200 I get a StackOverFlow. I tried changing to a long but that's all I could think of. Any idea as to how and why this is happening and how do I fix is so that it can calculate any number?

import java.util.Scanner;

public class Multiplication {

    public static long multiIterative(long a, long b) {
        long result = 0;
        while (b > 0) {
            result += a;
            b--;
        }
        return result;
    }

    public static long multiRecursive(long a, long b) {
        if (a == 0 || b == 0) {
            return 0;
        }
        return a + multiRecursive(a, b - 1);
    }

    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Please enter first Integer: ");
        long a = userInput.nextInt();
        System.out.print("Please enter second Integer: ");
        long b = userInput.nextInt();
        System.out.println("The Multiplication Iteration would be: " + multiIterative(a, b));
        System.out.println("The Multiplication Recursion would be: " + multiRecursive(a, b));
    }

}

Taken from http://en.wikipedia.org/wiki/Stack_overflow

The other major cause of a stack overflow results from an attempt to allocate more memory on the stack than will fit, for example by creating local array variables that are too large.

Juned Ahsan is right, you can try to increase the heap size to accomodate your recursion. check out this link:

Java stack overflow error - how to increase the stack size in Eclipse?

Recursion uses stack to store the function calls before reaching the termination condition, which causes the items from stack to pop out and generate the final result.

The size of the java function stack depends on the virtual memory allocated to the stack. You may try to fine tune it by setting an appropriate value for -Xss JVM parameter.

If recursion keeps going deeper and deeper depending on the input value then you should think about breaking down task.

In Java, each method call puts an activation record on the stack until it is completed. Recursion incurs as many activation records as method calls are made. Thus, a recursive algorithm cannot run indefinitely deep, as compared to an iterative design which under the hood essentially uses goto statements; thus it is one of the limitations of a recursive algorithm design.

Consider these two files: IterationTest.java will run forever happily (use ctrl + c to terminate execution of the file if running on a Bash terminal, such as in Linux), while RecursionTest.java will fail nearly immediately.

/*
 * Runs perfectly fine forever 
 * (use ctrl + c to escape execution in terminal, if on Linux)
 */
public class IterationTest
{
    public static void printMe()
    {
        while(true)
        {
            System.out.println("iteration");
        }
    }
    public static void main(String[] args)
    {
        printMe();
    }
}

/*
 * Guaranteed StackOverflow error using infinite recursion
 */
 public class RecursionTest
{
    public static void printMe()
    {
        System.out.println("iteration");
        printMe();
    }

    public static void main(String[] args)
    {
        printMe();
    }
}

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