gpt4 book ai didi

java - 为什么下面的java代码会出现StackOverflow错误?

转载 作者:行者123 更新时间:2023-12-01 16:52:01 25 4
gpt4 key购买 nike

执行以下命令时出现 StackOverflow 错误:

public class StackOverflow7 {
StackOverflow7 obj = new StackOverflow7();
int finalCount = 0;
public static void main(String[] args) {
for(int i = 1 ; i <= 5 ; i++)
System.out.println(i);

StackOverflow7 localObj = new StackOverflow7();
localObj.count(88);
System.out.println("Final Count :: " + localObj.finalCount);
}

private void count(int num){
finalCount = finalCount + num;
}
}

最佳答案

这一行:

StackOverflow7 obj = new StackOverflow7();
当您创建 StackOverflow7 的对象(您在这一行本身中执行此操作)时,

始终会被调用。因此,该行递归地调用自身,直到出现 StackOverflow 错误。

关于java - 为什么下面的java代码会出现StackOverflow错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com