Why can't Java use the same approach ?
I can't say which approach will be used, but it's better-explained in Project Loom's proposal:
As adding the ability to manipulate call stacks to the JVM will undoubtedly be required, it is also the goal of this project to add an even lighter-weight construct that will allow unwinding the stack to some point and then invoke a method with given arguments (basically, a generalization of efficient tail-calls). We will call that feature unwind-and-invoke, or UAI. It is not the goal of this project to add an automatic tail-call optimization to the JVM.
As far as I've heard, work has not yet begun on tail calls, as Fibers and Continuations seem to currently be a higher priority.
Answer from Jacob G. on Stack OverflowWhy can't Java use the same approach ?
I can't say which approach will be used, but it's better-explained in Project Loom's proposal:
As adding the ability to manipulate call stacks to the JVM will undoubtedly be required, it is also the goal of this project to add an even lighter-weight construct that will allow unwinding the stack to some point and then invoke a method with given arguments (basically, a generalization of efficient tail-calls). We will call that feature unwind-and-invoke, or UAI. It is not the goal of this project to add an automatic tail-call optimization to the JVM.
As far as I've heard, work has not yet begun on tail calls, as Fibers and Continuations seem to currently be a higher priority.
I read a very nice blog post here about how to achieve tail recursion in Java: Knoldus blog post on Java tail recursion
However, the code on their blog doesn't compile so I created a small repo with their code but fixed the syntax so it compiles. Github repo with working code
Hope this is useful to someone, I found the ideas presented at the Knoldus blog post very interesting.
EDIT: actually I found out later that the ideas presented in the blog post are originally Venkat Subramaniam's. He discusses these subjects in his talk here.
Tail Call Recursion in Java with ASM
compiler - Why doesn't Java have optimization for tail-recursion at all? - Software Engineering Stack Exchange
Is this tail recursion? - Question - Scala Users
Does Java support tail recursion? - Stack Overflow
As explained by Brian Goetz (Java Language Architect at Oracle) in this video:
in jdk classes [...] there are a number of security sensitive methods that rely on counting stack frames between jdk library code and calling code to figure out who's calling them.
Anything that changed the number of frames on the stack would break this and would cause an error. He admits this was a stupid reason, and so the JDK developers have since replaced this mechanism.
He further then mentions that it's not a priority, but that tail recursion
will eventually get done.
N.B. This applies to HotSpot and the OpenJDK, other VMs may vary.
Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops. The complexity isn't worth it for a feature whose use is discouraged as a matter of style.
This thing where programmers want to sometimes write in a FP-style in otherwise imperative languages only came into vogue in the last 10 years or so, after computers started scaling in cores instead of GHz. Even now, it's not that popular. If I suggested replacing an imperative loop with tail recursion at work, half the code reviewers would laugh and the other half would give a confused look. Even in functional programming, you generally avoid tail recursion unless other constructs like higher-order functions don't fit cleanly.