Most languages have compiler optimizations for tail recursion . Tail recursion means that the recursive call must be the last call of your recursive method. The compiler can then optimize this in a loop, preventing errors.
I'm not sure if all javac implementations implement tail recursion. This is not what is required in the specification. However, this is an important optimization method for any recursive program, so I assume that main thread implementations provide tail recursion.
You can test this yourself by taking a (simple) non-tail recursive program that throws a StackOverflowError and makes it tail recursive (by computing factorial , for example).
EDIT . Previously, there was a question about tail recursion in Java, as stated in a comment by sje397. Also take a look at Stephen C's answer to this question, which provides more information.
Ronald wildenberg
source share