Repository navigation
Expand file tree
/
Copy pathch_9_commonmistakes.ptx
More file actions
97 lines (78 loc) · 5.17 KB
/
Copy pathch_9_commonmistakes.ptx
File metadata and controls
97 lines (78 loc) · 5.17 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
<?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>
The 'cannot find symbol' error for the variable <c>count</c> on line 7 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>
<pre>
Main.java:7: error: cannot find symbol
count = new ArrayList<Integer>(10);
^
symbol: variable count
location: class Main
</pre>
</section>
<section xml:id="not-importing-a-class">
<title>Not importing a class</title>
<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>
<pre>
Histo.java:3: error: cannot find symbol
Scanner data = null;
^
symbol: class Scanner
location: class Histo
</pre>
</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>
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>
<pre>
Histo.java:8: error: cannot find symbol
data = Scanner(new File("test.dat"));
^
symbol: method Scanner(File)
location: class Histo
</pre>
</section>
<section xml:id="forgetting-a-semicolon">
<title>Forgetting a Semicolon</title>
<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>
<pre>
Histo.java:7: error: ';' expected
Scanner data = null
^
</pre>
</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>
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>
<pre>
UncheckedWarningDemo.java:8: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
rawList.add("Hello");
^
where E is a type-variable:
E extends Object declared in class ArrayList
</pre>
</section>
</chapter>