Results 1 to 3 of 3
Like Tree2Likes
  • 2 Post By jashburn

Thread: Question Regarding Error

  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Rep Power

    Default Question Regarding Error

    The code:
    Java Code:
    public boolean makeBricks(int small, int big, int goal) {
      if(goal == 0) return true;
      if(small > 0 && big > 0) {
      return makeBricks(small - 1, big, goal - 1) || makeBricks(small, big - 1, goal - 5);
      if(small > 0)
      return makeBricks(small - 1, big, goal - 1);
      if(big > 0) return makeBricks(small, big - 1, goal - 5);
      return false;

    Input of this:
    makeBricks(1000000, 1000, 1000100)

    gives me Exception:java.lang.StackOverflowError (line number:4)

    Input of this:
    makeBricks(2, 1000000, 100003)

    gives me Exception:java.lang.StackOverflowError (line number:8)

    I've been working with Python recently, so I decided to grab some easy recursion stuff from codingbat to maintain my java knowledge.

    Why am I getting these errors? As far as I understand, its because the numbers are simply too big? It works on all smaller inputs.
    Last edited by AlexGraal; 03-27-2014 at 04:13 AM.

  2. #2
    thesloth is offline Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: Question Regarding Error

    The total amount of stuff on the stack exceeds that default value, so you get that error.

    let consider the case: makeBricks(1000000, 1000, 1000100)
    from line 4, it will call makeBricks(999999, 1000, 1000099) and makeBricks(1000000,999, 1000095)
    inside they will both call another two makeBricks
    then numbers of call will be 1+2^2
    So, the class will call at least 2^1000times to reach one end makeBricks(?,0,?)
    But it is not the final end of the program and the class will still go on and go on.......

    therefore you better edit your program to reduce the number of the calling-class loop

    This is only my view, so correct me if i am wrong
    Last edited by thesloth; 03-27-2014 at 05:41 AM.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: Question Regarding Error

    This is a great question that deserves further study. The (very) short answer is big numbers cause the method calls to recurse too deeply exceeding the size of the call stack. See StackOverflowError (Java Platform SE 7 ) for the official one-liner on this error.

    The relationship between recursion and the call stack is well-described in Part I of this article: Data Structures: Recursion, Stacks, And Trees - Java Tutorials | Dream.In.Code.

    There's recursion, and there's recursion. See Java Recursion with examples on the different types of recursion. Some programming languages perform tail recursion optimisation/elimination to prevent errors like this (see Tail Call Optimization) but unfortunately Java does not, nor does Python (see Neopythonic: Tail Recursion Elimination).

    Finally, see Inspired by Actual Events: Diagnosing and Resolving StackOverflowError on the different ways in which StackOverflowError can occur.
    gimbal2 and AlexGraal like this.

Similar Threads

  1. Array; error question?
    By wheehoowaffles in forum New To Java
    Replies: 4
    Last Post: 04-06-2013, 03:01 AM
  2. Simple question on why getting this error
    By dloexcitement in forum New To Java
    Replies: 9
    Last Post: 04-28-2012, 01:22 PM
  3. Replies: 7
    Last Post: 01-01-2012, 01:29 PM
  4. Applet error question
    By pure67muscle in forum Java Applets
    Replies: 0
    Last Post: 01-25-2011, 04:03 PM
  5. Replies: 5
    Last Post: 06-22-2010, 04:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts