Understanding the Java stack

前端 未结 7 1233
无人及你
无人及你 2020-12-24 06:44

There is this code:

public class Main {
    public static void main(final String[] args) throws Exception {
        System.out.print(\"1\");
        doAnythi         


        
7条回答
  •  情歌与酒
    2020-12-24 07:25

    One thing is clear that System.out.print("y"); in catch creates this puzzle. If we change the code as

    static int n;
    
    public static void main(final String[] args) throws Exception {
        System.out.println("1");
        doAnything();
        System.out.println(n);
    }
    
    private static void doAnything() {
        try {
            doAnything();
        } catch (Error e) {
            n++;
        }
    }
    

    it prints

    1
    1
    

提交回复
热议问题