Search code examples
javainfinite-loopstack-overflow

Stack Overflow error vs. Infinite loop


I know what an Infinite Loop error is. Is a stack overflow error the same thing. If not, what is the difference?

Can you give example code as well?


Solution

  • If, instead of infinite loop, you have infinite (or very deep) recursion (function invoking itself), then you will get stack overflow. Whenever a function is invoked, some part of stack memory is consumed. Once all the stack is exhausted, you get - stack overflow error.