Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Infinte loop and Recursive function

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

I have simple doubt about infinte loop and recursive funtion with the StackOverflowError.

My question is why infinite loop will not cause StackOverFlowError whereas Recursive function will?

E.g in the SCJP book i have found a below question and at first i thought one of the option will be StackOverflowError but it was wrong.

Could someone please explain this?

case 0 : {
for(int x=10;x>5;x++){
if(x>10000000000)x=10;
break;
}
}

the two closely related options for this are

1.StackOverflowError
2.Program Might hang without ever completing

this will surely cause infinte loop when the switch valus i 0 but why the option is just only 2 not include 1.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When one method calls another, it gets added to the stack.
When it completes, it comes off the stack.
If the stack gets too big, it overflows.

So with recursion you can get a StackOverFlow as the method never actually completes; it is always calling itself and adding itself to the stack until the overflow happens (unless you have guards in place, of course).

With an infinite loop, the code just goes round and round, never stopping.
If any methods are called, they get added to the stack but then removed again when they complete.
So no stack overflow, but you may get lots of other problems depending on the exact code.

The code you show is a simple infinite loop. When x reaches 10,000,000,000 it is reset to 10; thus x>5 never fails and it just keeps going.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason told you all what you have to know, BUT your code is NOT an infinite loop. Because of your break statement it exits the loop right away.

cheers
Bob
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hate badly formatted code!
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Jason on his explanation and I would like to add something with regard the int x that keeps increasing, this value of x does NOT keep occupying more space in the memory (stack) as the number x grows large. Its size is already set when the variable was first declared as int, so for every increment of the value x, the value changes and occupies the same place that the previous value was occupying thus the previous value is lost. When the value of x grows larger than what int can handle in 32bit then the value of x starts from the opposite end of the cycle.

On the other hand, method calls as in recursive functions keep occupying more space in the stack for each invocation and since the stack is limited then StackOverFlow will ultimately occur as result of repetitive calls.

Cheers!!!
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:Jason told you all what you have to know, BUT your code is NOT an infinite loop. Because of your break statement it exits the loop right away.

cheers
Bob



Hi
Sorry it was my mistake

the actual code is an infinte loop the break statement is not for "for loop" it is for case block.

here is the actual code

case 0: {
for(int x=10;x>10;x++)
if(x>10000000000)x=10;
break;
}
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, and this piece of code will cause your program to hang.
StackOverflow won't be in that case - we do not put anything on the stack in this loop
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siva, please use code blocks. There is a button labelled "Code" in the post editor for this purpose.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will not compile because 10000000000 is incorrect int literal (too large).
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vlado Zajac wrote:The code will not compile because 10000000000 is incorrect int literal (too large).


Right but 10000000000 was only the example
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vlado Zajac wrote:The code will not compile because 10000000000 is incorrect int literal (too large).


The example is taken from Sierra&Bates, Chapter 5 quiz, question 9. The value used in the question is "10000000" (10,000,000). As this is less that 2^31-1 it is a valid integer and the code will enter an infinite loop.

But you perfectly correct Vlado - if the question appeared in the exam with "10000000000" (10,000,000,000) we'd be looking at a compilation error, which is not one of the 7 choices available in the book.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic