File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What are StackOverflowErrors? When are they typically thrown? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "What are StackOverflowErrors? When are they typically thrown?" Watch "What are StackOverflowErrors? When are they typically thrown?" New topic
Author

What are StackOverflowErrors? When are they typically thrown?

Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71


Hi,
The above code throws a stackoverflowerror.

Have read a few posts that explained that stack errors are typically thrown due to an endless loop.

1. Any other nuances.

Also i had come across a question on a website which i am trying to trace back.

The code in the question was somewhat like below


The above code just a brief recollection.

2.But the question i had in mind is can using chained iterators on a collection lead to some sort of StackOverflow which the website said would happen.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3047
    
    1

You can figure out for yourself when a stack overflow occurs, if you know what the stack is.

The stack is a piece of memory that your program uses to store all the local variables in a method. Every time you call a method, Java adds a so called "stack frame" to the stack, this frame holds the local variables for the method you just called. When the method is finished, that frame is removed from the stack again, revealing the stack frame belonging to the calling method, so that method can resume execution.

The stack overflows when it has no more space to add the next stack frame. This happens when a method calls another method, and that method calls another method, etc; until there is no more memory. You will usually encounter this problem when you make a recursive method call.

In your first program, you can see that the main function calls doSomething(String) at line 12. This method in turn calls the main function, at line 34. This is a never ending loop, which keeps pushing frames on the stack, but the methods never get a chance to finish.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: What are StackOverflowErrors? When are they typically thrown?
 
Similar Threads
Doubt about sub-super class relationship
please explain why the compiler is not complaining and why the output is 0
Inner classes
Collections iteration behavior
Static variables and inheritance