|
1 | 1 | <?xml version="1.0" encoding="UTF-8"?> |
2 | 2 | <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> |
4 | 6 |
|
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. |
10 | 15 | </p> |
11 | 16 | <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. |
13 | 18 | </p> |
14 | 19 | <p> |
15 | 20 | Here is the standard implementation in Python: |
|
24 | 29 | else: |
25 | 30 | return n * factorial(n - 1) |
26 | 31 |
|
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) |
28 | 35 | </code> <tests> </tests> |
29 | 36 | </program> |
30 | 37 | <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: |
32 | 43 | </p> |
33 | | - <program language="java"> |
| 44 | + |
| 45 | + <program interactive="activecode" language="java"> |
34 | 46 | <code> |
35 | 47 | public class Main { |
36 | 48 | // A recursive function to calculate the factorial of a number. |
|
43 | 55 | return n * factorial(n - 1); |
44 | 56 | } |
45 | 57 |
|
| 58 | + // Let's calculate the factorial of 5. |
46 | 59 | 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); |
50 | 64 | } |
51 | 65 | } |
52 | 66 | </code> |
53 | 67 | </program> |
54 | 68 | <p> |
55 | 69 | 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>. |
56 | 70 | </p> |
57 | | - </introduction> |
58 | 71 | </section> |
59 | 72 |
|
60 | 73 | <section xml:id="java-patterns-for-recursion"> |
61 | | - <title>Common Recursive Patterns in Java</title> |
62 | | - <introduction> |
| 74 | + <title>Common Recursive Patterns</title> |
| 75 | + |
63 | 76 | <p> |
64 | 77 | 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. |
65 | 78 | </p> |
|
94 | 107 | <p> |
95 | 108 | This public/private helper pattern is a cornerstone of good recursive design in Java, providing both efficiency and a clean interface. |
96 | 109 | </p> |
97 | | - </introduction> |
98 | 110 | </section> |
99 | 111 |
|
100 | 112 | <section xml:id="recursion-limits-in-java"> |
|
0 commit comments