简体   繁体   English

使用StackOverFlow进行奇怪的Java乘法递归

[英]Strange Java Multiplication Recursion with StackOverFlow

The Java program functions correctly using iteration and recursion. Java程序使用迭代和递归正确运行。 But for some strange reason I can't understand is that when the numbers I enter with anything above 9200 I get a StackOverFlow. 但由于一些奇怪的原因,我无法理解的是,当我输入9200以上的数字时,我得到一个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 取自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. Juned Ahsan是对的,你可以尝试增加堆大小以适应你的递归。 check out this link: 看看这个链接:

Java stack overflow error - how to increase the stack size in Eclipse? Java堆栈溢出错误 - 如何在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. java函数堆栈的大小取决于分配给堆栈的虚拟内存。 You may try to fine tune it by setting an appropriate value for -Xss JVM parameter. 您可以尝试通过为-Xss JVM参数设置适当的值来-Xss进行-Xss

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. 在Java中,每个方法调用都会在堆栈上放置一条激活记录,直到完成为止。 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; 因此,与基本上使用goto语句的迭代设计相比,递归算法不能无限深地运行; 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. 考虑这两个文件: IterationTest.java将永远运行愉快(如果在Bash终端上运行,使用ctrl + c终止文件的执行,例如在Linux中),而RecursionTest.java几乎会立即失败。

/*
 * 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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