Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note the absence of newline characters in <code>433943394339</code>. It indicates that something wrong happens inside <code>System.out.println()</code>.</p> <p>The essential point here is that <code>System.out.println()</code> requires some stack space to work, so that <code>StackOverflowError</code> is thrown from <code>System.out.println()</code>.</p> <p>Here is your code with marked points:</p> <pre><code>public static void main(String[] args) { try{ System.out.println(i); // (1) i++; main(args); // (2) }catch (StackOverflowError e){ System.out.println(i); // (3) i++; main(args); // (4) } } </code></pre> <p>Let's imagine what happens at level N of recursion when i = <code>4338</code>:</p> <ul> <li>Statement (1) at level N prints <code>4338</code>. <strong>Output <code>4338\n</code></strong></li> <li><code>i</code> is incremented to <code>4339</code></li> <li>Control flow enters level N + 1 at (2)</li> <li>Statement (1) at level N + 1 tries to print <code>4339</code>, but <code>System.out.println()</code> throws a <code>StackOverflowError</code> before it prints a newline. <strong>Output <code>4339</code></strong></li> <li><code>StackOverflowError</code> is caught at level N + 1, statement (3) tries to print <code>4339</code> and fails for the same reason again. <strong>Output <code>4339</code></strong></li> <li>Exception is caught at level N. At this point there is more stack space available, therefore statement (3) tries to print <code>4339</code> and succeeds (newline is printed correctly). <strong>Output <code>4339\n</code></strong></li> <li><code>i</code> is incremented and control flow enters level N + 1 again at (4)</li> </ul> <p>After this point the situation repeats with <code>4340</code>. </p> <p>I'm not sure why some numbers are printed correclty between sequences without newlines, perhaps its related to internal work of <code>System.out.println()</code> and buffers it uses.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload