Repository navigation
Expand file tree
/
Copy pathch9_commonmistakes.ptx
More file actions
157 lines (125 loc) · 8.42 KB
/
Copy pathch9_commonmistakes.ptx
File metadata and controls
157 lines (125 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?xml version="1.0"?>
<!-- Generated by Docutils 0.19 -->
<chapter xml:id="common-mistakes-id1">
<title>Common Mistakes</title>
<introduction>
</introduction>
<section xml:id="forgetting-to-declare-your-variables">
<title>Forgetting to declare your variables</title>
<p>
In Python, you can use a variable without declaring it first, but in Java, you must declare all variables before using them.
If you try to use a variable that has not been declared, the Java compiler will give you an error message like this:
</p>
<program language="java" interactive="activecode" line-numbers="yes">
<code>
import java.util.ArrayList; // Import necessary class
public class Main { // Class declaration
// Main method declaration
public static void main(String[] args) {
// Attempt to use 'count' without declaration
count = new ArrayList<Integer>(10);
// This line won't be reached due to the error.
System.out.println("ArrayList created.");
} // End of main method
} // End of class
</code>
</program>
<p>
The 'cannot find symbol' error for the variable <c>count</c> on line 6 indicates that <c>count</c> was used before it was declared within the <c>Histo</c> class. In Java, all variables must be explicitly declared with a data type (e.g., <c>int</c>, <c>String</c>, <c>ArrayList<Integer></c>) before they can be assigned a value or referenced in any way. The arrow in the error message points to where the undeclared variable <c>count</c> was first encountered. To resolve this, <c>count</c> needs to be declared with its appropriate type (e.g., <c>ArrayList<Integer> count;</c>) before any attempt to initialize or use it.
</p>
</section>
<section xml:id="not-importing-a-class">
<title>Not importing a class</title>
<p>
In Python, many classes are available by default. However, in Java, you must explicitly import most classes from external packages that you want to use.
</p>
<program language="java" interactive="activecode" line-numbers="yes">
<code>
// Histo.java
public class Histo { // Class declaration
public static void main(String[] args) { // Main method declaration
// This line will cause the "cannot find symbol" error
Scanner data = null;
} // End of main method
} // End of class
</code>
</program>
<p>
You may notice that this error message looks similar to the previous one, however, it has an entirely different cause. In Java, classes like <c>Scanner</c> that are part of external packages (like <c>java.util</c>) must be explicitly imported into your source file. Java does not automatically recognize these classes. To resolve this error, you need to add an import statement for the <c>Scanner</c> class at the beginning of your <c>Histo.java</c> file, typically import <c>java.util.Scanner;</c>.
</p>
</section>
<section xml:id="forgetting-to-use-the-new-keyword-to-create-an-object">
<title>Forgetting to use the new keyword to create an object</title>
<p>
Unlike Python, where you can create a new object without explicitly using a keyword, Java requires the <c>new</c> keyword to instantiate a new object.
</p>
<program language="java" interactive="activecode" line-numbers="yes">
<code>
// Histo.java // The filename for this example
import java.io.File; // Import the File class, needed for file operations
import java.util.Scanner; // Import the Scanner class, now correctly imported
public class Histo { // Class declaration
public static void main(String[] args) { // Main method declaration
Scanner data = Scanner(new File("test.dat")); // This Line will cause an error
} // End of main method
} // End of class
</code>
</program>
<p>
This error message occurs when you forget to use the <c>new</c> keyword to instantiate an object.
Specifically, on line 8 of <c>Histo.java</c>, <c>data = Scanner(new File("test.dat"));</c> leads to a 'cannot find symbol' error.
While the message states 'symbol: method Scanner(File)', this can be misleading.
Java incorrectly interprets <c>Scanner()</c> as an attempt to call a static method named <c>Scanner</c> within the <c>Histo</c> class
(or an inherited one). However, <c>Scanner</c> is a constructor of the <c>Scanner</c> class, not a standalone method,
and therefore requires the new keyword (e.g., <c>data = new Scanner(new File("test.dat"));</c>) to create a new <c>Scanner</c> object.
</p>
</section>
<section xml:id="forgetting-a-semicolon">
<title>Forgetting a Semicolon</title>
<p>
A common mistake in Java is to forget that every statement must end with a semicolon (<c>;</c>).
</p>
<program language="java" interactive="activecode" line-numbers="yes">
<code>
// Histo.java
import java.util.Scanner; // Imports Scanner
public class Histo { // Class declaration
public static void main(String[] args) { // Main method declaration
Scanner data = null // The error will point here
System.out.println("This line will not compile.");
}// End of main method
}//End of class
</code>
</program>
<p>
The error "';' expected" on line 7 of <c>Histo.java</c> means that a semicolon is missing at the end of the statement <c>Scanner data = null</c>. In Java, every statement must be terminated with a semicolon (<c>;</c>) to indicate its completion. The arrow points to <c>null</c> because that's where the compiler expected to find the semicolon.
</p>
</section>
<section xml:id="forgetting-to-declare-the-kind-of-object-in-a-container">
<title>Forgetting to declare the kind of object in a container</title>
<p>
Java is a statically typed language, meaning you must specify the type of objects that can be stored in a container like an <c>ArrayList</c>. If you forget to declare the type, the compiler will give you an error.
</p>
<program language="java" interactive="activecode" line-numbers="yes">
<code>
// UncheckedWarningDemo.java
import java.util.ArrayList; // Import the ArrayList class
public class UncheckedWarningDemo { // Class declaration
public static void main(String[] args) { // Main method declaration
// This is where the potential for unchecked operations begins.
ArrayList rawList = new ArrayList();
rawList.add("Hello");
//This is the line that will throw an error
System.out.println("Element added: " + rawList.get(0));
} // End of main method
} // End of class
</code>
</program>
<p>
This is a compiler warning, not an error, indicating a potential type safety issue. It occurs because you are calling the <c>add()</c> method on <c>rawList</c>, which is an <c>ArrayList</c> used as a raw type (i.e., without specifying a generic type like <c><String></c> or <c><Integer></c>).
</p>
<p>
When <c>ArrayList</c> is used as a raw type, the compiler cannot guarantee the type of elements being added or retrieved, leading to "unchecked" operations. The <c>E extends Object</c> part refers to the generic type parameter <c>E</c> that <c>ArrayList</c> should have, which defaults to <c>Object</c> when the type is not specified, making the <c>add()</c> call unchecked. This can lead to <c>ClassCastExceptions</c> at runtime if incompatible types are later retrieved and cast.
</p>
</section>
</chapter>