Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    What is overflow?

    package Number_package;

    public class NumberUtilis {

    public int a;
    public int b;
    public int sum;
    public int largest;
    public int smallest;
    public int average;

    public NumberUtilis(int a, int b) {
    this.a = a;
    this.b = b;
    this.sum = sum;
    this.largest = largest;
    this.smallest = smallest;
    this.average = average;
    }

    public int sum() {
    int sum = a + b;
    return sum();
    }

    public int largest() {
    if (a > b) largest = a;
    else largest = b;
    return largest;
    }

    public int smallest() {
    if (a < b) smallest = a;
    else smallest = b;
    return smallest;
    }

    public int average() {
    int average = (a + b)/2;
    return average();
    }
    }




    import Number_package.NumberUtilis;

    class using_NumberUtilis {

    public static void main (String [] args) {

    NumberUtilis s = new NumberUtilis(2, 3);

    System.out.println(s.sum());
    System.out.println(s.largest());
    System.out.println(s.smallest());
    System.out.println(s.average());
    }
    }

    When I run these it says, Exception in thread "main" java.lang.StackOverflowError. Can someone explain it to me please? Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    'Stack' is you're function stack. Its overflowing since its constantly adding onto the stack, without removing from it. Essentially, you're chewing up all of you're available memory for function use.
    This is common in incorrectly handled recursive functions, or circular functions. In you're case, its caused by an incorrect circular function:
    Code:
    public int sum() {
    int sum = a + b;
    return sum();
    }
    // and
    public int average() {
    int average = (a + b)/2;
    return average();
    }
    These two methods return the result of calling themselves, which in themselves call another, and so forth. The correct results are return sum; and return average;
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    alien2000 (11-17-2008)

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks a lot.... really helpful........

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    np mate,
    Thats why they keep me around
    I don't normally hang out too much in the Java forum, but I've been doing a little more java work lately, and I feel more comfortable with answering questions correctly.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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