You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch7_recursion.ptx
+16-9Lines changed: 16 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -273,23 +273,30 @@ public class ArrayProcessor {
273
273
274
274
<sectionxml:id="recursion-limits-in-java">
275
275
<title>Recursion Limits: Python vs. Java</title>
276
-
<p>
277
-
The consequence of deep recursion, running out of stack space, is a concept you may have already encountered in Python. Java handles this in a very similar way to Python, throwing an error when the call stack depth is exceeded.
When using recursion, both Python and Java have practical limits on how deep the recursion can go before running into errors. This is due to the way both languages manage something called the <term>call stack</term>, which is a limited amount of memory used to keep track of function or method calls.
278
278
</p>
279
279
<p>
280
-
The key difference is the name of the error:
281
-
</p>
280
+
The consequence of running out of call stack space, is a concept you may have already encountered in Python. Java handles this in a very similar way to Python, both throwing an error when the call stack depth is exceeded.
<li>In Python, this raises a <c>RecursionError</c>.</li>
284
-
<li>In Java, this throws a <c>StackOverflowError</c>.</li>
285
+
<li>In Python, overflowing the call stack raises a <c>RecursionError</c> error.</li>
286
+
<li>In Java, it throws a <c>StackOverflowError</c>.</li>
285
287
</ul>
288
+
</p>
289
+
<aside>
290
+
<p>
291
+
<idx>tail-call optimization</idx><idx>TCO </idx><term>Tail-call optimization (TCO)</term> is a technique used in programming to improve the efficiency of recursive function or method calls when the recursive call is the very last operation. This helps prevent some stack overflow errors, especially in languages that support automatic tail call optimization. Unfortunately, neither Python nor Java support automatic tail-call optimization.</p>
292
+
</aside>
286
293
<p>
287
-
Neither language supports tail call optimization, so the practical limits on recursion depth are a factor in both. If an algorithm requires thousands of recursive calls, an iterative, loop-based, approach is likely going to be the preferred solution in both Python and Java.
294
+
In both languages, if you write a recursive function that doesn't have a base case or that just recurses too deeply, you'll eventually hit this limit. When this happens, Python will raise a <c>RecursionError</c>, while Java will throw a <c>StackOverflowError</c>. This is because both languages use a call stack to keep track of function calls, and when the stack runs out of space, it results in an error.
295
+
Hence, when an algorithm might require thousands of recursive calls, an iterative, loop-based, approach is likely going to be the preferred solution in both Python and Java.
288
296
</p>
289
-
<p><idx> Tail Call Optimization </idx> <term>Tail Call Optimization</term> (TCO) is a technique used in programming to improve the efficiency of recursive function calls by reusing the current function's stack frame instead of creating a new one. This helps prevent stack overflow errors and reduces memory usage, especially in languages that support it.</p>
290
297
291
298
<p>
292
-
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a <c>RecursionError</c>.
299
+
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a <c>RecursionError</c> due to overflowing the call stack.
0 commit comments