2

I heard one of my colleagues saying that Java handles recursion more efficiently than c or C++, I was just curious what why is it able to do so? I mean what is "under the hood" process that makes this happen.

All efforts appreciated.

koool
  • 15,157
  • 11
  • 45
  • 60
  • Could you ask him to elaborate? – Thorbjørn Ravn Andersen Sep 07 '11 at 17:15
  • I'm confused as to how Java could be more "efficient" than C in any way. – Travis Webb Sep 07 '11 at 17:18
  • 3
    Any reasons they provide? I could see Java being able to optimize better for some code due to JIT, but there are also optimizations C/C++ compilers make that the JVM doesn't, like [tail-call optimization](http://en.wikipedia.org/wiki/Tail_call). – wkl Sep 07 '11 at 17:22
  • @Travis Webb : I agree with you but for handling recursion it is .... I also came across while reading some books too ... but I never knew why – koool Sep 07 '11 at 17:23
  • This [question](http://stackoverflow.com/questions/233013/how-does-your-favorite-language-handle-deep-recursion) might help. – CoolBeans Sep 07 '11 at 17:23
  • 3
    Clearly the correct answer is in [this question](http://stackoverflow.com/questions/7337816/why-is-java-better-at-handling-recursion). :) – James Manning Sep 07 '11 at 17:24
  • @birryee I didnt ask them we were just talking and it suddenly came to my head now .... so I thought of posting a question maybe some one knows the answer – koool Sep 07 '11 at 17:25
  • 1
    @birryree - I don't know the current state of JVM/Java compilers, but [this SO question](http://stackoverflow.com/questions/105834/does-the-jvm-prevent-tail-call-optimizations) from 3 years ago states the JVM (at least at the time) did support the specific case of self-recursive (which in my experience is the vast majority of recursive functions actually written). The security reasons around not doing so for recursion 'cycles' that were larger than a single function made sense, although since that's 3 years old, they may have long since been solved. – James Manning Sep 07 '11 at 17:27
  • 1
    Why was this question closed? In my opinion, it fits perfectly to the Q&A format in the FAQ. Some so-users seem to make it a sport nowadays to close questions. And I was just typing a very detailed answer... – DaveFar Sep 07 '11 at 17:28
  • @James Manning - nice link, I also learned the tail-call problem from reading "Programming Scala", so I took that as canon. – wkl Sep 07 '11 at 17:30
  • I dunno why ppl closed this question ... this can provide people with in depth understanding of the core working .... but I thank all who put their effort to answer my question – koool Sep 07 '11 at 17:32
  • @DaveBall aka user750378 If you could type your answer in the comments that would be great – koool Sep 07 '11 at 17:33
  • 1
    Sorry, my answer's gone. But everything is also said in http://stackoverflow.com/questions/105834/does-the-jvm-prevent-tail-call-optimizations, which someone mentioned here already. – DaveFar Sep 07 '11 at 17:39
  • Java is more efficient that C++ in some ways, the most obvious being dynamic binding (which C++ doesn't have). Java for example in line virtual methods of libraries which are not available at compile time. However, for most things C++ is faster and recursion is one example. Something Java lacks is tail call optimisation, which for most trivial cases makes a big difference. – Peter Lawrey Sep 07 '11 at 18:29

2 Answers2

3

The usual issue around recursion (not 100% sure this is what your colleague was referring to) is whether 'it' (the compiler, the JIT, the runtime, whatever) can (and does) implement 'tail call optimization'. The goal is that instead of having the code make 'real' calls (introducing a new frame onto the call stack) that recurse (either into the same function or through the same 'cycle' of functions), you can get the same effect without doing so.

The wikipedia page is pretty decent on describing it.

http://en.wikipedia.org/wiki/Tail_call

James Manning
  • 13,429
  • 2
  • 40
  • 64
1

If it's correct it's because the JIT compilation is able to optimize a recursion better than the C compiler. http://en.wikipedia.org/wiki/Just-in-time_compilation