在 try-catch 中来自无限递归的令人困惑的输出
2022-08-31 19:55:15
						请考虑以下代码。
public class Action {
private static int i=1;
public static void main(String[] args) {
    try{
        System.out.println(i);
        i++;
        main(args);
    }catch (StackOverflowError e){
        System.out.println(i);
        i++;
        main(args);
    }
 }
}
我正在正确地获得价值。捕获输出后,按如下方式接线。4338StackOverflowError
4336
4337
4338 // up to this point out put can understand 
433943394339 // 4339 repeating thrice  
434043404340
4341
434243424342
434343434343
4344
4345
434643464346
434743474347
4348
434943494349
435043504350
在这里考虑现场演示。它工作正常,直到 。这到底是怎么回事呢?i=4330
仅供参考:
我按照代码做了操作,以了解这里发生了什么。
public class Action {
    private static int i = 1;
    private static BufferedWriter bw;
    static {
        try {
            bw = new BufferedWriter(new FileWriter("D:\\sample.txt"));
        } catch (IOException e) {
           e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        bw.append(String.valueOf(i)+" ");
        try {
            i++;
            main(args);
        } catch (StackOverflowError e) {
            bw.append(String.valueOf(i)+" ");
            i++;
            main(args);
        }
    }
}
现在上一个问题不存在了。现在的值最高校正和运行进一步。我正在跳跃,这可能会运行到(整数的最大值)的值,而不会出现问题。i16824744i=2,147,483,647
存在一些问题。下面也有类似的答案。但是为什么?println()
实际原因是什么?