Skip to content

Commit 92dd726

Browse files
committed
make Python and Java more parallel
1 parent 1d1272f commit 92dd726

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

‎source/chx-recursion.ptx‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<chapter xml:id="recursion-in-java">
3-
<title>Recursion in Java: A Transition from Python</title>
3+
<title>Recursion in Java</title>
4+
<introduction>
5+
</introduction>
46

5-
<section xml:id="translating-recursion-to-java">
6-
<title>Translating Your Recursive Logic to Java</title>
7-
<introduction>
8-
<p>
9-
As you know from Python, recursion is a powerful problem-solving technique involving base cases and recursive steps. When moving to Java, the core logic you've learned remains identical. The challenge is adapting that logic to Java's statically-typed, class-based syntax.
7+
<section xml:id="basic-recursion">
8+
<title>Basic Recursion</title>
9+
<p>
10+
In this chapter, we will explore how to translate your recursive logic from Python to Java. While the core concepts of recursion remain the same, the syntax and structure of your code will change somewhat.
11+
12+
</p>
13+
<p><idx>recursion</idx>
14+
As you may know from Python, <term>recursion</term> is a powerful problem-solving technique involving base cases and recursive steps in which a function or method calls itself. When moving to Java, the core logic you've learned remains identical. The challenge is adapting that logic to Java's statically-typed, class-based syntax.
1015
</p>
1116
<p>
12-
Let's take the familiar factorial function. The logical steps are the same, but the implementation details change.
17+
Let's take the familiar factorial function (which calculates the factorial of a number, namely the product of all positive integers from 1 to n). The logical steps in the code are the same, but the implementation details change.
1318
</p>
1419
<p>
1520
Here is the standard implementation in Python:
@@ -24,13 +29,20 @@
2429
else:
2530
return n * factorial(n - 1)
2631

27-
print(f"5! is {factorial(5)}")
32+
# Let's calculate the factorial of 5.
33+
x = 5
34+
print("The factorial of " + x + " is: " + result)
2835
</code> <tests> </tests>
2936
</program>
3037
<p>
31-
To write this in Java, you must place the function (now called a method) inside a class and declare the data types for its parameters and return value.
38+
The Java version follows the same recursive logic but requires three key syntax changes: the method must be inside a class, you must declare the parameter and return types (<c>int n</c> and <c>int return</c>), and you use <c>public static</c> to make it callable from <c>main</c>. The base case and recursive step remain conceptually identical.
39+
</p>
40+
41+
<p>
42+
Here is the equivalent Java code:
3243
</p>
33-
<program language="java">
44+
45+
<program interactive="activecode" language="java">
3446
<code>
3547
public class Main {
3648
// A recursive function to calculate the factorial of a number.
@@ -43,23 +55,24 @@
4355
return n * factorial(n - 1);
4456
}
4557

58+
// Let's calculate the factorial of 5.
4659
public static void main(String[] args) {
47-
int n = 3; // Let's calculate the factorial of 3.
48-
int result = Main.factorial(n); // Call the existing factorial method.
49-
System.out.println("The factorial of " + n + " is: " + result); // Output: The factorial of 3 is: 6
60+
int x = 5;
61+
int result = Main.factorial(x); // Call the existing factorial method.
62+
// Print the result.
63+
System.out.println("The factorial of " + x + " is: " + result);
5064
}
5165
}
5266
</code>
5367
</program>
5468
<p>
5569
Notice the key differences: instead of <c>def</c>, the method signature <c>public static int</c> declares its visibility, that it belongs to the class rather than an object, and that it returns an <c>int</c>. All logic is contained within curly braces <c>{}</c>.
5670
</p>
57-
</introduction>
5871
</section>
5972

6073
<section xml:id="java-patterns-for-recursion">
61-
<title>Common Recursive Patterns in Java</title>
62-
<introduction>
74+
<title>Common Recursive Patterns</title>
75+
6376
<p>
6477
You're likely familiar with how some recursive algorithms, like the naive Fibonacci implementation, are elegant but inefficient due to redundant calculations. In Java, a common and robust pattern to solve this is using a private helper method. This separates the clean, public-facing API from the more complex internal recursion.
6578
</p>
@@ -94,7 +107,6 @@
94107
<p>
95108
This public/private helper pattern is a cornerstone of good recursive design in Java, providing both efficiency and a clean interface.
96109
</p>
97-
</introduction>
98110
</section>
99111

100112
<section xml:id="recursion-limits-in-java">

0 commit comments

Comments
 (0)