Results 1 to 8 of 8
Thread: Looping and Recurring
- 12-21-2009, 09:37 PM #1
Looping and Recurring
Hi everyone. :)
I've got a question about looping and recurring in Java. Take a look at the two programs below:
Application A
Java Code:public class Main { public static void main(String[] args) { while (true) { System.out.println(": p"); } } }
Java Code:public class Main { public static void main(String[] args) { go(); } public static void go() { System.out.println(": p"); go(); } }
Application C
Java Code:#include "stdafx.h" #include <iostream> using namespace std; void go(); void main(int argumentCount, char **aguments) { go(); cin.get(); } void go() { cout << ": p\n"; go(); }
Just something that caught my attention.
Thank you. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 12-21-2009, 09:54 PM #2
Recursion uses the System stack. Every call puts another entry into the stack. Eventually, the stack gets full and then you have a stack overflow error. Recursion is not the same as looping, rather, its a means of keeping a placeholder and calling a method from itself. In order to be useful, recursive methods must have a Base Case as well as a recursive call.
- 12-21-2009, 09:56 PM #3
Oh yeah, FYI, the 'Base Case' is the termination condition to prevent infinite calls. For example:
Java Code:public class Main { public static void main(String[] args) { go(10); } public static void go(int n) { //Base case if(n == 0) return; System.out.println(": p"); go(n-1); } }
- 12-21-2009, 10:32 PM #4
Thank you quad64bit for your comment. I've learned that our machines are implementations of finite state machines and because of this they cannot "see into the future" and prevent stack overflows like these. So there's no way to prevent this except by reducing the problem to a finite one. I mean, what if the problem is a near-infinite problem, like traversing a really big tree? Then, do we just get a bigger computer? ;)
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 12-21-2009, 10:35 PM #5
"Then, do we just get a bigger computer?" Yes :D In the case of java, just add more ram to the JVM.
- 12-22-2009, 09:09 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Most (if not all) functional languages remove tail recursion, i.e. if the last statement of a function is a call to (another?) function no 'subroutine' call is made but a simple 'jump' after erasing its own stack frame. Java isn't one of them so the stack will grow forever with this scenario.
kind regards,
Jos
- 12-22-2009, 08:42 PM #7
- 12-22-2009, 08:52 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
looping and filtering
By javafanatic in forum New To JavaReplies: 14Last Post: 02-09-2010, 09:48 AM -
looping problems
By Blakester in forum New To JavaReplies: 4Last Post: 10-05-2009, 08:26 PM -
Looping problem
By Tanilo in forum New To JavaReplies: 1Last Post: 08-01-2008, 06:34 AM -
Looping ArrayList
By hai789 in forum Web FrameworksReplies: 5Last Post: 05-07-2008, 03:55 AM -
looping a function
By Username in forum New To JavaReplies: 2Last Post: 07-30-2007, 05:37 PM
Bookmarks