好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java StackOverflowError详解

StackOverflowError

原因 : 函数调用栈太深了,注意代码中是否有了循环调用方法而无法退出的情况

原理

StackOverflowError 是一个java中常出现的错误:在jvm运行时的数据区域中有一个java虚拟机栈,当执行java方法时会进行压栈弹栈的操作。在栈中会保存局部变量,操作数栈,方法出口等等。jvm规定了栈的最大深度,当执行时栈的深度大于了规定的深度,就会抛出StackOverflowError错误。

典型的例子:

?

1

2

3

4

5

6

7

8

9

10

public class StackOverFlowDemo {

 

     public static void Foo(){

         Foo();

     }

 

     public static void main(String[] args) {

         Foo();

     }

}

今天我遇见了另外一种情况:当两个对象相互引用,在调用toString方法时会产生这个异常,因为它们会循环调用toString方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

//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()方法,在打印一个对象时,会先调用这个对象的toString()方法,例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/gentlezuo/article/details/90580116

查看更多关于Java StackOverflowError详解的详细内容...

  阅读:20次