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
+18-17Lines changed: 18 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -256,7 +256,7 @@ public class ArrayProcessor {
256
256
public static void main(String[] args) {
257
257
int[] numbers = {1, 2, 3, 4, 5};
258
258
int result = sumArray(numbers);
259
-
System.out.println("The sum of [1, 2, 3, 4, 5] is " + result);
259
+
System.out.println("The sum of " + Arrays.toString(numbers) + " is " + result);
260
260
}
261
261
}
262
262
</code>
@@ -274,7 +274,7 @@ public class ArrayProcessor {
274
274
<sectionxml:id="recursion-limits-in-java">
275
275
<title>Recursion Limits: Python vs. Java</title>
276
276
<p>
277
-
The consequence of deep recursion, running out of stack space, is a concept you've already encountered in Python. Java handles this in a very similar way, throwing an error when the call stack depth is exceeded.
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.
278
278
</p>
279
279
<p>
280
280
The key difference is the name of the error:
@@ -284,10 +284,10 @@ public class ArrayProcessor {
284
284
<li>In Java, this throws a <c>StackOverflowError</c>.</li>
285
285
</ul>
286
286
<p>
287
-
Neither language supports <idx> tail call optimization </idx><term>tail call optimization</term>, 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 the preferred solution in both Python and Java.
287
+
Neither language supports <idx> tail call optimization </idx><term>tail call optimization</term>, 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.
288
288
</p>
289
289
<p>
290
-
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a RecursionError.
290
+
The following Python code demonstrates a situation where a function calls itself indefinitely without a base case, leading to a <c>RecursionError</c>.
@@ -297,32 +297,33 @@ public class ArrayProcessor {
297
297
"""
298
298
cause_recursion_error()
299
299
300
-
# Standard Python entry point
301
-
if __name__ == "__main__":
302
-
print("Calling the recursive function... this will end in an error!")
303
-
304
-
# This line starts the infinite recursion.
305
-
# Python will stop it and raise a RecursionError automatically.
306
-
cause_recursion_error()
300
+
print("Calling the recursive function... this will end in an error!")
301
+
302
+
# The line below will start the infinite recursion.
303
+
# Python will stop it and raise a RecursionError automatically.
304
+
# Each call adds a new layer to the program's call stack.
305
+
# Eventually, the call stack runs out of space, causing the error.
306
+
cause_recursion_error()
307
307
</code>
308
308
</program>
309
309
310
310
<p>
311
-
The following Java code demonstrates a similar situation, where a method calls itself indefinitely without a base case, leading to a StackOverflowError.
311
+
The following Java code demonstrates a similar situation, where a method calls itself indefinitely without a base case, leading to a <c>StackOverflowError</c>.
0 commit comments