This is because the parser is recursively processing the if-else branches. This
is one huge if statement. It's basically equivalent to
if (true) {
print(x);
} else {
if (true) {
print(x);
} else {
if (true) {
print(x);
} else {
…
}
}
so the parser code even
Nashorn throws a StackOverflowError if there are a few hundred if else
statements in a function.
function whyDoesThisThrowStackOverflowError(x) {
if (true) { print(x); }
else if (true) { print(x); }
else if (true) { print(x); }
else if (true) { print(x); }
else if (true) { print(x); }