3

Sorry, for probably a dumb question, I'm new to Java.

Is there a way to make an endless recursion in Java, somithing like:

public void sillyMethod()
{
    System.out.println(i);
    i++;
    sillyMethod();

}

it throws StackOverflowError, but I really want to run it endless. Is there any way to make it?

Thanks!

rosx
  • 31
  • 2
  • If you really want "endless", go for iteration. – Anon. Jan 31 '11 at 21:54
  • Read about [Stack Overflows](http://en.wikipedia.org/wiki/Stack_overflow) and you will see that you can't – Sean Patrick Floyd Jan 31 '11 at 21:54
  • @Anon Iteration? That wouldn't be endless, would it? I mean, that's the whole point of iteration, you count something – Sean Patrick Floyd Jan 31 '11 at 21:55
  • 1
    related: http://stackoverflow.com/questions/105834/does-the-jvm-prevent-tail-call-optimizations – Bozho Jan 31 '11 at 21:56
  • 1
    @Sean: I use iteration as basically synonymous with loops. Additionally, just because you're counting doesn't mean you ever stop... `for(i = 0; ; i++) ;` loops forever in any language with silent wraparound on overflows. – Anon. Jan 31 '11 at 21:57
  • @Anon I guess my definition of iterating is more semantically equivalent to what the Iterable interface in Java does, but yours seems to be the correct definition – Sean Patrick Floyd Jan 31 '11 at 22:02

4 Answers4

5

Yes and no, (but mostly no! :)

No, it's not possible (the most sensible answer):
For every call, there will be an activation record pushed onto the JVM call stack. This takes a non-zero amount of memory, thus you will at some point run out of memory, at which point a StackOverflowException will be thrown.

Yes, it is possible (the super-theoretical answer):
There is nothing in the Java Language Specification that explicitly says that you should eventually run into a StackOverflowException. This means that if you find a cleaver enough compiler, it may be intelligent enough to compile this into a loop.


A related question would be, "Does the JVM support tail-call optimization." The answer to this question is, "no, not at moment, but it's not ruled out for future versions".

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • ...then the activation record would be slightly larger. But if the recursion can be transformed into a loop, what does it matter? – aioobe Jan 31 '11 at 22:20
  • *"... clever enough compiler ..."* the real technical challenge in Java is implementing tail-call optimization in a way that does not break the Java security model, etc. – Stephen C Jan 31 '11 at 22:39
  • 1
    @Stephen C, could you give an example of how it is related to the security model? – aioobe Jan 31 '11 at 22:42
1

As others above have said, infinite recursion will eventually lead to a stack overflow, at least as far as the JVM implementation is concerned.

You could do something like this, which is similar, but avoids the stack expansion by spawning a new thread right before the old one dies.

public class SillyClass implements Runnable {

private final int count;

public SillyClass(int cnt) {
    this.count = cnt;
}

public static void main(String[] args) {
    Thread t = new Thread(new SillyClass(0));
    t.start();
}

@Override
public void run() {
    System.out.println(count);
    Thread t = new Thread(new SillyClass(count + 1));
    t.start();
}

}

JohnnyO
  • 3,018
  • 18
  • 30
0

Not recursively, no. It does imply creating an ever-increasing call stack, and eventually you will run out of memory to contain it.

0

With Java, you cannot do this. However, tail call optimization in languages (esp. functional ones such as Ocaml), you can do this since it internally turns it into a loop.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43