Solution to 7e2a: Stack overflow
See code at solutions/code/tutorialquestions/question7e2a
Examine class StackOverflow
and compare it with your solution.
Running class StackOverflow
from the sample solution three times in succession, I get the following output:
Number of recurseive calls to 'overflow' before stack overflow: 7312
Number of recurseive calls to 'overflow' before stack overflow: 7294
Number of recurseive calls to 'overflow' before stack overflow: 7320
This demonstrates that, even on a single platform, the maximum amount of stack space varies between runs of a Java program. This is because of unpredictable, behind-the-scenes execution by the runtime environment, most notably the garbage collector.
The message is that the programmer can make no assumptions about the precise size of the runtime stack. (Though of course, we do make assumptions about the size of the runtime stack all the time: we assume generally that there is plenty of it!)