The question says it all. I was taking a look at Can a recursive function be inline? so trying to correlate that to Java.
Asked
Active
Viewed 875 times
1 Answers
2
Sort of. Tail-recursive methods can be fairly readily converted to loops. Otherwise, the JITC may inline several calls as a sort of "unrolling".
Depends on the phase of the moon and the day of the week, though -- lots of different factors affect the JITC's decisions.

Hot Licks
- 47,103
- 17
- 93
- 151
-
But if they are converted to loops, won't that lose values of local variables per stack frame? Or does JITC takes care of that also? – shrini1000 Mar 16 '12 at 12:35
-
If it can't be guaranteed to have the same result (including side effects), then the JITter can't do it. But there's nothing saying additional temporary variables can't be introduced doing the optimizations. – user Mar 16 '12 at 12:38
-
4In fact, Java JIT Compilers **can't** convert tail calls into loops. – Stephen C Mar 16 '12 at 12:38
-
@shrini1000 -- Note that a tail-recursive method has no local variables that need to be preserved. This is why it can be so readily converted to a loop. – Hot Licks Mar 16 '12 at 15:24
-
@HotLicks - see http://bugs.sun.com/view_bug.do?bug_id=4726340 -- the bug responses explain why this is NOT a trivial thing to implement. – Stephen C Mar 16 '12 at 23:50
-
@StephenC -- That analysis is wrong -- no bytecode change is needed, and the traceback can be made correct. I remember that the protection domain stuff required some head scratching, but was no big deal once it was figured out. – Hot Licks Mar 17 '12 at 00:10
-
@HotLicks - details please. What JITC are you actually talking about? (By the way "... requires some head scratching" means the same thing as "... not a trivial thing to implement".) – Stephen C Mar 17 '12 at 09:16
-
@StephenC -- The "classic" JVM for IBM iSeries. For several years held top honors for performance on several benchmarks. "Requires head scratching" means that it takes awhile to get your head around the problem, but once we did the "fix" was nearly trivial. – Hot Licks Mar 17 '12 at 13:56