Ruc*_*era 53 java stack-overflow
请考虑以下代码.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了4338
正确的价值.捕获StackOverflowError
输出后如下连线.
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
Run Code Online (Sandbox Code Playgroud)
在这里考虑现场演示.它正常工作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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在上一期不存在.现在的价值i
高达16824744
改正,并进一步运行.我正在跳跃这可能会达到i=2,147,483,647
(int的最大值)值而没有问题.
有一些问题println()
.下面也有类似的答案.但为什么?
实际原因是什么?
axt*_*avt 52
请注意,缺少换行符433943394339
.它表明内部发生了错误System.out.println()
.
这里的关键点是System.out.println()
需要一些堆栈空间才能工作,因此StackOverflowError
抛出它System.out.println()
.
这是带有标记点的代码:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
让我们假设当i =时,在递归的N级发生了什么4338
:
4338
.产量4338\n
i
递增到 4339
4339
,但在打印换行符之前System.out.println()
抛出一个StackOverflowError
.产量4339
StackOverflowError
在N + 1级别被捕获,语句(3)尝试打印4339
并再次以相同的原因失败.产量4339
4339
并成功(正确打印换行符).产量4339\n
i
增加并且控制流量在(4)处再次进入水平N + 1在此之后情况重演4340
.
我不确定为什么有些数字在没有换行符的序列之间打印相关,也许它与System.out.println()
它使用的内部工作和缓冲区有关.
Mar*_*aux 17
我怀疑发生的是这样的:
归档时间: |
|
查看次数: |
2697 次 |
最近记录: |