1

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));
    }

}
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • What don't you understand currently? – user253751 Feb 20 '15 at 01:26
  • I punch in say for the first integer say 50,000 and for the second integer 50,000 again. when I enter it in I get a StackOverFlow warning. I want to be able to calculate 50,000 x 50,000. I don't understand why it doesn't want to calculate that. –  Feb 20 '15 at 01:29
  • 1
    @PatrickWilliamMcCully stack overflow occurs because your application recurses too deeply – Johny Feb 20 '15 at 01:29
  • how can it recurse to deeply. So are you saying that its not possible to calculate that much? –  Feb 20 '15 at 01:31
  • @PatrickWilliamMcCully have you ever seen a StackOverflowError before now? (with a problem like infinite recursion maybe) – user253751 Feb 20 '15 at 01:33

3 Answers3

2

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?

Community
  • 1
  • 1
jmcg
  • 1,547
  • 17
  • 22
1

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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

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();
    }
}
aaroncarsonart
  • 1,054
  • 11
  • 27