Does Java 8 have tail call optimization?
Asked Answered
R

2

16

I tried digging on the web to get my question answered. I found some documents related to Project DaVinci. This is tagged to the JSR 292 which is related to including closures in the JVM. Did this project get realized and is it a part of Java 8?

Reckoner answered 4/4, 2014 at 15:9 Comment(0)
G
11

As far as I know Java 8 does not have tail call optimization. Afaik it isn't related to the actual compiler trick, because that one is simple, but to preserve a callstack for security purposes. But I guess it would be possible with a bytecode rewriter.

Gaffney answered 4/4, 2014 at 15:13 Comment(1)
The security issue is that Java keeps track of its security context by unwinding the current stack trace. You have to be very careful about optimizations that rewrite the call stack and potentially allow callees to escape their security contexts.Dogma
N
8

Java does not support TCO at compiler level but it is possible to implement it with Java 8 using lambda expressions. It is described by Venkat Subramaniamin in "Functional Programming in Java".

Nilson answered 27/8, 2015 at 7:57 Comment(4)
Thanks will check it outReckoner
Just checked it out. It is awesome.Reckoner
Please quote the relevant excerpt from the book.Backbreaking
@Backbreaking blog.agiledeveloper.com/2013/01/… .Here is a blog created by Venkat which talks about thisReckoner

© 2022 - 2024 — McMap. All rights reserved.