Skip to content

Commit dea854b

Browse files
committed
Adding Introduction and Debugging Section in Chapter 9
1 parent ceb3402 commit dea854b

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

source/ch9_commonmistakes.ptx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,45 @@
77
<introduction>
88
</introduction>
99

10+
<section xml:id="how-to-avoid-mistakes">
11+
<title>How to Avoid Making Mistakes</title>
12+
<p>
13+
Making mistakes is a natural part of learning Java—or any programming language. The good news is that most errors happen for just a few common reasons, and once you recognize the patterns, they become much easier to fix. This chapter focuses on those typical mistakes and how to understand and correct them.
14+
</p>
15+
<p>
16+
One of the best ways to avoid these errors is to slow down and test your code in small pieces. Write a few lines, compile, and check the output before moving on. If something goes wrong, read the error message carefully and focus on fixing one problem at a time. Often, solving the first error helps fix others that follow.
17+
</p>
18+
<p>
19+
A simple debugging technique is to use <c>System.out.println()</c> to print out variable values and program flow. If you're not sure whether a part of your code is running or what a variable contains, print it out. This helps you check your assumptions and narrow down where something is going wrong.
20+
</p>
21+
<program language="java" interactive="activecode" line-numbers="yes">
22+
<code>
23+
// DebugExample.java
24+
public class DebugExample {
25+
26+
public static void main(String[] args) {
27+
int number = 10;
28+
int result = multiplyByTwo(number);
29+
// Debugging: print the result to verify the method worked
30+
System.out.println("Result after multiplying: " + result);
31+
}
32+
33+
public static int multiplyByTwo(int value) {
34+
// Debugging: print the input value to check it's being passed correctly
35+
System.out.println("multiplyByTwo received: " + value);
36+
return value * 2;
37+
}
38+
}//End of class
39+
</code>
40+
</program>
41+
<p>
42+
In the example above, <c>System.out.println()</c> is used inside both <c>main</c> and <c>multiplyByTwo()</c> to trace what values are being passed and returned. This kind of print-based debugging can quickly reveal logic errors, unexpected behavior, or whether a method is even being called.
43+
</p>
44+
<p>
45+
Above all, be patient with yourself. Every mistake you make is an opportunity to understand Java more deeply.
46+
</p>
47+
</section>
48+
1049
<section xml:id="forgetting-a-semicolon">
1150
<title>Forgetting a Semicolon</title>
1251
<p>

0 commit comments

Comments
 (0)