You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch_4_javadatatypes.ptx
+27-3Lines changed: 27 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -212,7 +212,20 @@ public class TempConv {
212
212
For Python programmers, the following error is likely to be even more common. Suppose we forgot the declaration for <c>cel</c> and instead left line 6 blank. What would happen when we type <c>javac TempConv.java</c> on the command line?
213
213
</p>
214
214
215
-
<pre>TempConv.java:13: cannot find symbol symbol : variable cel location: class TempConv cel = (fahr - 32) * 5.0/9.0; ^ TempConv.java:14: cannot find symbol symbol : variable cel location: class TempConv System.out.println("The temperature in C is: " + cel); ^ 2 errors</pre>
215
+
<pre>
216
+
TempConv.java:13: cannot find symbol
217
+
symbol : variable cel
218
+
location: class TempConv
219
+
cel = (fahr - 32) * 5.0/9.0;
220
+
^
221
+
TempConv.java:14: cannot find symbol
222
+
symbol : variable cel
223
+
location: class TempConv
224
+
System.out.println("The temperature in C is: " + cel);
225
+
^
226
+
2 errors
227
+
</pre>
228
+
216
229
<p>
217
230
When you see the first kind of error, where the symbol is on the left side of the equals sign, it usually means that you have not declared the variable. If you have ever tried to use a Python variable that you have not initialized the second error message will be familiar to you. The difference here is that we see the message before we ever try to test our program. More common error messages are discussed in the section <xrefref="common-mistakes-id1"/>.
218
231
</p>
@@ -522,7 +535,11 @@ public class Histo {
522
535
Technically, you don’t <em>have</em> to declare what is going to be in an array list. The compiler will allow you to leave the <c><``*Type*</c>>`` off the declaration. If you don’t tell Java what kind of object is going to be on the list Java will give you a warning message like this:
523
536
</p>
524
537
525
-
<pre>Note: Histo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.</pre>
538
+
<pre>
539
+
Note: Histo.java uses unchecked or unsafe operations.
540
+
Note: Recompile with -Xlint:unchecked for details.
541
+
</pre>
542
+
526
543
<p>
527
544
Without the <c><Integer></c> part of the declaration Java simply assumes that <em>any</em> object can be on the list. However, without resorting to an ugly notation called casting, you cannot do anything with the objects on a list like this! So, if you forget you will surely see more errors later in your code. (Try it and see what you get)
528
545
</p>
@@ -531,7 +548,14 @@ public class Histo {
531
548
Lines 13—20 are required to open the file. Why so many lines to open a file in Java? The additional code mainly comes from the fact that Java forces you to reckon with the possibility that the file you want to open is not going to be there. If you attempt to open a file that is not there you will get an error. A try/catch construct allows us to try things that are risky, and gracefully recover from an error if one occurs. The following example shows the general structure of a try/catch block.
532
549
</p>
533
550
534
-
<pre>try { Put some risky code in here, like opening a file } catch (Exception e) { If an error happens in the try block an exception is thrown. We will catch that exception here! }</pre>
551
+
<pre>
552
+
try {
553
+
Put some risky code in here, like opening a file
554
+
} catch (Exception e) {
555
+
If an error happens in the try block an exception is thrown. We will catch that exception here!
556
+
}
557
+
</pre>
558
+
535
559
<p>
536
560
Notice that in line 16 we are catching an <c>IOException</c>. In fact, we will see later that we can have multiple catch blocks to catch different types of exceptions. If we want to be lazy and catch any old exception we can catch an <c>Exception</c> which is the parent of all exceptions. However, catching <c>Exception</c> is a terrible practice, since you may inadvertently catch exceptions you do not intend to, making it harder to identify bugs in your program.
Copy file name to clipboardExpand all lines: source/ch_6_loopsanditeration.ptx
+12-8Lines changed: 12 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -42,20 +42,24 @@ public class DefiniteLoopExample {
42
42
Recall that the <c>range</c> function provides you with a wide variety of options for controlling the value of the loop variable.
43
43
</p>
44
44
45
-
<pre>range(stop)
46
-
range(start,stop)
47
-
range(start,stop,step)</pre>
45
+
<pre>
46
+
range(stop)
47
+
range(start,stop)
48
+
range(start,stop,step)
49
+
</pre>
48
50
49
51
<p>
50
52
The Java <c>for</c> loop is really analogous to the last option giving you explicit control over the starting, stopping, and stepping in the three clauses inside the parenthesis.
0 commit comments