-1

I created a recursion program to print a message Hello several times, but failed with StackOverflowError in the compilation.

package com.recre;

public class Recursionhello {
    static void p() {
        System.out.println("Hello");
    p();
}

public static void main(String[] args) {
    p();
    }
}

It is printing the output “Hello” several times and then it is printing the following error message.

out put -

Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Exception in thread "main" java.lang.StackOverflowError
 at java.io.FileOutputStream.write(Unknown Source)
 at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
 at java.io.BufferedOutputStream.flush(Unknown Source)
 at java.io.PrintStream.write(Unknown Source)
 at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
 at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
 at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source)
 at java.io.OutputStreamWriter.flushBuffer(Unknown Source)
 at java.io.PrintStream.write(Unknown Source)
 at java.io.PrintStream.print(Unknown Source)
 at java.io.PrintStream.println(Unknown Source)
 at com.recre.Recursionhello.p(Recursionhello.java:5)
 at com.recre.Recursionhello.p(Recursionhello.java:6)
 at com.recre.Recursionhello.p(Recursionhello.java:6)
 at com.recre.Recursionhello.p(Recursionhello.java:6)
 at com.recre.Recursionhello.p(Recursionhello.java:6)
 at com.recre.Recursionhello.p(Recursionhello.java:6)
 at com.recre.Recursionhello.p(Recursionhello.java:6)
 at com.recre.Recursionhello.p(Recursionhello.java:

Here I need assistant on the error and some explanation on the recursion.

  • Any recursive method must have a termination condition. Otherwise, it simply runs until the stack overflows. What is the desired terminating condition of the recursive call? – KevinO Sep 01 '18 at 07:01
  • This is not necessarily true. What the OP wrote is a perfectly natural way for a beginner to write an endless loop. It is much simpler to write, much simpler to understand, and uses fewer concepts than a `while` or `for` loop, and it does not require mutation. And since it is a tail-call, more specifically, a tail-recursive call, it should run in constant space, and in fact *does* run in constant space in many programming languages (e.g. Scala, Scheme, Erlang, Haskell, Racket, F♯, ML). The problem, really, is that Java does not have Proper Tail Calls or at least Proper Tail Recursion. – Jörg W Mittag Sep 01 '18 at 07:09
  • @JörgWMittag, while what you state is true, I take the statement in the posed question of print "several times" to indicate it was not an attempt to write an endless loop. – KevinO Sep 01 '18 at 07:17

2 Answers2

1

Basically, recursion is a way where a method call itself which makes a loop. Any loop needs a condition to terminate. In this case you are missing the condition of termination of this loop. With respect to the Stack Overflow error, when you call a method it gets loaded on the stack and stack is limited to 256 KB. Each method call consumes some memory on stack and when the size of stack is filled and we try again to load another method on stack by calling itself we get StackOverflowError.

0

Any recursive program in Java must have exit condition, like this:

public class Recursionhello {
    static void p(int times) {
        System.out.println("Hello");
        if(times > 0) {
            p(times - 1);
        }
    }

    public static void main(String[] args) {
        p(5);
    }
}
Max Farsikov
  • 2,451
  • 19
  • 25
  • This is not necessarily true. What the OP wrote is a perfectly natural way for a beginner to write an endless loop. It is much simpler to write, much simpler to understand, and uses fewer concepts than a `while` or `for` loop, and it does not require mutation. And since it is a tail-call, more specifically, a tail-recursive call, it should run in constant space, and in fact *does* run in constant space in many programming languages (e.g. Scala, Scheme, Erlang, Haskell, Racket, F♯, ML). The problem, really, is that Java does not have Proper Tail Calls or at least Proper Tail Recursion. – Jörg W Mittag Sep 01 '18 at 07:09
  • @Max without parameters what is the stack size for the above method? – suvethagan ganes Sep 01 '18 at 07:12
  • @suvethaganganes, first of all it doesn't matter, because your program has a flaw, and it should be fixed. Second: try to google. – Max Farsikov Sep 01 '18 at 07:16