Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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.

• '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;`

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

alien2000 (11-17-2008)

• Thanks a lot.... really helpful........

• 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.

•

#### Posting Permissions

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