L
L
lordekar2015-10-05 23:29:00
Java
lordekar, 2015-10-05 23:29:00

What is the condition for throwing a StackOverflowError?

Let's say we have a method

public static void foo(){
    foo();
}

Run and throw a StackOverflowError. The docs say: "Thrown when a stack overflow occurs because an application recurses too deeply." What does too deep mean, how does Java understand this limit? 1000, 10000 investments?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2015-10-05
@lordekar

Well, it is obvious that the compiler has a maximum allowable size of the call stack.
When a function is called, a certain set of data is pushed onto the stack. With normal (non-tail) recursion, each nested function call will increase the stack by N bytes. At a certain point (at which point it depends on several factors, including the number and types of function parameters, the maximum stack size), the stack will have nowhere to grow, and this error will occur.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question