• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StackOverflowError

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is a question from one of the enthuware tests asking the result of the below code:

What will the following code print?



The answer is that it will run but never terminate.

I say that the getX() method will cause a StackOverflowError.


I ran it for over 10 seconds and received nothing.

Shouldn't it logically throuw the StackOverflowError exception?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shouldn't it logically throuw the StackOverflowError exception?



Well, I am assuming you have a reason why it should "logically" overflow the stack. To overflow the stack, you must actually use the stack (in a way that will cause an overflow). Can you explain to us why you think the stack will overflow?

Henry
[ March 30, 2008: Message edited by: Henry Wong ]
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

why do you think there should happen a stack overflow?

This code has an endless loop in method looper() because the condition of the while loop is always true (getX() always returns 5 != 0). Therefore it's true that the program never terminates.

But an endless loop doesn't necessarily mean that there will be a stack overflow. This would be typically the case with recursive methods (which getX() actually isn't). With recursive method calls the calling stack grows until it explodes if there's no condition which terminates the recursion early enough.

Marco
reply
    Bookmark Topic Watch Topic
  • New Topic