Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

What is Stack Overflow Error in Java ?

I am trying to implement a recursive function to calculate factorials and then an equation. A Stack Overflow Error keeps on recurring. What is this error and how to resolve it ?

6 Answers

Relevance
  • AirMc
    Lv 4
    8 years ago
    Favorite Answer

    Infinite recursion(or too many recursion!)

    For every function call, something called activation record is placed on the stack.

    Stack overflow means there is not enough room on the stack for another activation record.

    Solution: may be it is your algorithm

    factorial(n)

    if (n == 1) return 1 else return n*factorial(n-1)

  • 8 years ago

    Each time that a recursive function calls itself, it needs a little more room on the stack. A stack overflow is generally an infinite recursion. Make sure that your recursion has a way to end. For factorials, you'll have something like this:

    if (n <= 0) { // make sure that you always terminate here for zero and for negative numbers

    return 1;

    } else {

    return n * factorial(n-1); // make sure that you subtract 1 here

    }

  • Siju
    Lv 4
    8 years ago

    Parameters and local variables are allocated on the stack (with reference types the object lives on the heap and a variable references that object). The stack typically lives at the upper end of your address space and as it is used up it heads towards the bottom of the address space (ie towards zero).

    Your process also has a heap, which lives at the bottom end of your process. As you allocate memory this heap can grow towards the upper end of your address space. As you can see, there is the potential for the heap to "collide" with the stack (a bit like techtonic plates!!!).

    The common cause for a stack overflow is a bad recursive call. Typically this is caused when your recursive functions doesn't have the correct termination condition, so it ends up calling itself for ever. However, with gui programming it's possible to generate indirect recursion. For example, your app may be handling paint messages and whilst processing them it may call a function that causes the system to send another paint message. Here you've not explicitly called yourself, but the OS/VM has done it for you.

    To deal with them you'll need to examine your code. If you've got functions that call themselves then check that you've got a terminating condition. If you have then check than when calling the function you have at least modified one of the arguments, otherwise there'll be no visible change for the recusivly called function and the terminating condition is useless.

  • 8 years ago

    While using a recursive function, a Stack Overflow Error occors mainly when the method is called too many times. This error is similar to the infinite loop in cause of a loop.

    You need to ensure that the the method gets terminated once it is used. You should have posted the method so that someone can figure out why it does not terminate.

    however if none of your answers solver your problem, you can sent it to me to: live.neville@live.com

    Source(s): done java for a few years.
  • How do you think about the answers? You can sign in to vote the answer.
  • 5 years ago

    It means that line 27 in JumpingBug calls move, and that when it gets to line 27 again it calls move again. At so e point Java runs out of memory, stack memory, and so gives up and throws an exception. Presumably line 27 should be guarded by a conditional statement that at some point does not make the reclusive call. Alternatively perhaps you just calling the wrong function there and did not intend recursion at all.

  • 8 years ago

    The arrays you must have used must be inappropriate or some pointers used could have an issue. Vary difficult to say if the code is not given. Visit the below link, it might help.

Still have questions? Get your answers by asking now.