3

It is just a funny question. Not a real code for production. I do not want to fix it. I just want to understand this strange behavior. I have the code that should print "1" in each line. Actually, it is false. I get the strange result like "11111111" in one line.

class Scratch
{
  public static void main( String[] args )
  {
    method();
  }

  static void method()
  {
    try
    {
      System.out.println(1);
      method();
    }
    catch ( StackOverflowError e )
    {
      method();
    }
  }
}

Output could be the following:

1
11111
1
11111
1
1
1
1
1
11111111
1
11111
Paul
  • 580
  • 1
  • 7
  • 22
  • 2
    You are calling your method recursively and when it errors on an overflow you still call it recursively. Why are you not looping to get the result – DTul Mar 14 '19 at 15:00
  • 2
    Because your code is broken. You continue to print after an overflow. – vincrichaud Mar 14 '19 at 15:05
  • 3
    Unlimited recursion + ignored exception == weird – khelwood Mar 14 '19 at 15:07
  • 1
    Please, read the question. I do NOT need a fix. I know that method is incorrect. I just want to understand this strange behavior. – Paul Mar 14 '19 at 15:10
  • Well as NovaBomb states, it's probably your output trying to make sense of printing even after stack limits have been reached. – DTul Mar 14 '19 at 15:11
  • 2
    Probably platform and implementation dependant. `PrintStream` (`System.out`) uses an internal buffer and prints that to the console. Seems like that the *writes* to the buffer occur faster than it can be flushed, thus you get some accumulations of `1111` in a single line. (I have no way to confirm this) – Lino Mar 14 '19 at 15:25
  • @Lino Agree, but do you have any ideas where are missed extra line breaks? – Paul Mar 14 '19 at 15:28
  • 1
    @Paul print and newline are two different operations in `PrintStream`, `println` just combines them both, I guess that the newline is just very slow to apply, so multiple prints accumulate beforehand – Lino Mar 14 '19 at 15:30

3 Answers3

3

While, as @khelwood said in a comment, you shouldn’t have any specific expectations to a program that ignores repeated StackOverflowErrors. About anything may go wrong.

However, an attempt at a not too unlikely explanation: System.out.println(1) consists of printing 1 and the platform-specific new-line sequence — on Windows again consisting of printing \r and \n. There is nothing stopping the stack overflow from happening between the two or when trying to print the newline after the 1 has been successfully printed. In these cases the next 1 (if successful) will be printed on the same line.

Your output seems to show about 30 1 and about 12 (full) newlines. So apparently the scenario I sketched has happened a little more than half of the times. If this is the correct explanation, which we don’t know.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    *fun fact*: the longer the program runs the more `1`s are printed on a single line, seems that java can't really recover that fast – Lino Mar 14 '19 at 15:46
0

Curiously tried to work on your code, feeling Daredevilish today..

If you add a System.out.println(2); in catch you can see that the result now shows that the 1 are being printed in single lines, its the ones from catch and the ones triggering the error which ignore the cursor move from println

static void method() {
    try {
        System.out.println(1);
        method();
    } catch (StackOverflowError e) {
        System.out.println(2);
        method();
    }

}

Output:

1
1
122
122
1
122
122
1
1
Mischiefz
  • 127
  • 16
-2
public class Test {

 static int count = 0; //counter

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

  static void method()
  {
    try
    {
        if (count < 10) //Stop code after 10 runs
        {
            System.out.println(1);
            count++; //increments count
            method();
        }
    }
    catch ( StackOverflowError e )
    {
      method();
    }
  }
}

Your problem is you can not do recursion without a if or limiting factor. In the code above, I use a count to prevent the coding from printing infinity

Remember any time recursion is used it must have a condition to tell it when to stop

EDIT: As to your printing on multiple lines. It's most likely due to the computer printing infinitely fast enough that it overlaps in places

Novabomb
  • 99
  • 1
  • 10