axtavt Answer is very complete, but I would like to add this:
As you know, the stack is used to store the memory of variables, based on which you cannot create new variables when you reach the limit, it is true that System.out.println will require several stack resources
787 public void More ...println(Object x) { 788 String s = String.valueOf(x); 789 synchronized (this) { 790 print(s); 791 newLine(); 792 } 793 }
Then, after calling the print, the error does not even allow you to call a new line, it again breaks directly to the print. Based on this, you can verify this by changing your code as follows:
public class Action { static int i = 1; public static void main(String[] args) { try { System.out.print(i + "\n"); i++; main(args); } catch (StackOverflowError e) { System.out.print(i + " SO " + "\n"); i++; main(args); } } }
Now you will not ask the stack to process new lines, you will use the constant "\ n", and you can add some debugging to the exception print line, and your output will not have multiple values ββon one line:
10553 10553 SO 10553 SO 10554 10554 SO 10554 SO 10555 10556 10557 10558
And it will continue to be interrupted until several resources are allocated to distribute new data and move on to the next value of i.
porfiriopartida Sep 09 '13 at 19:10 2013-09-09 19:10
source share