Python 官方文档:入门教程 => 点击学习
StackOverflowError 原因 : 函数调用栈太深了,注意代码中是否有了循环调用方法而无法退出的情况 原理 StackOverflowError 是一个java中常出现的
原因 : 函数调用栈太深了,注意代码中是否有了循环调用方法而无法退出的情况
StackOverflowError 是一个java中常出现的错误:在JVM运行时的数据区域中有一个java虚拟机栈,当执行java方法时会进行压栈弹栈的操作。在栈中会保存局部变量,操作数栈,方法出口等等。jvm规定了栈的最大深度,当执行时栈的深度大于了规定的深度,就会抛出StackOverflowError错误。
典型的例子:
public class StackOverFlowDemo {
public static void Foo(){
Foo();
}
public static void main(String[] args) {
Foo();
}
}
今天我遇见了另外一种情况:当两个对象相互引用,在调用toString方法时会产生这个异常,因为它们会循环调用toString方法。
//book和student相互循环引用
public class StackOverFlowDemo {
static class Student{
String name;
Book book;
public Student(String name) {
this.name = name;
}
//循环调用toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", book=" + book +
'}';
}
}
static class Book {
String isbn;
Student student;
public Book(String isbn, Student student) {
this.isbn = isbn;
this.student = student;
}
@Override
public String toString() {
return "Book{" +
"isbn='" + isbn + '\'' +
", student=" + student +
'}';
}
}
public static void main(String[] args) {
Student student=new Student("zhang3");
Book book=new Book("1111",student);
student.book=book;
System.out.println(book.toString());
}
}
出现的错误:
说到toString()方法,在打印一个对象时,会先调用这个对象的toString()方法,例如:
public class toStringDemo {
static class A{
@Override
public String toString() {
System.out.print("I ");
return "";
}
}
public static void main(String[] args) {
A a=new A();
System.out.println("love you."+a);
}
}
会输出:
I love you.
到此这篇关于Java StackOverflowError详解的文章就介绍到这了,更多相关Java StackOverflowError内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java StackOverflowError详解
本文链接: https://www.lsjlt.com/news/132551.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0