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/ch3_javadatatypes.ptx
+21-9Lines changed: 21 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -527,8 +527,8 @@ void main() {
527
527
Next, let’s look at a program which reads numbers from a file and produces a histogram showing the frequency of the numbers. The data file we will use has one number between 0 and 9 on each line of the file. Here is a simple Python program that creates and prints a histogram.
Before going any further, I suggest you try to compile the above program and run it on some test data that you create.
@@ -674,10 +678,14 @@ public class Histo {
674
678
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:
675
679
</p>
676
680
677
-
<pre>
681
+
<listingxml:id="java-arraylist-warning">
682
+
<program>
683
+
<code>
678
684
Note: Histo.java uses unchecked or unsafe operations.
679
685
Note: Recompile with -Xlint:unchecked for details.
680
-
</pre>
686
+
</code>
687
+
</program>
688
+
</listing>
681
689
682
690
<p>
683
691
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)
@@ -686,14 +694,18 @@ public class Histo {
686
694
<p>
687
695
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.
688
696
</p>
689
-
690
-
<pre>
697
+
698
+
<listingxml:id="java-try-catch">
699
+
<program>
700
+
<code>
691
701
try {
692
702
Put some risky code in here, like opening a file
693
703
} catch (Exception e) {
694
704
If an error happens in the try block an exception is thrown. We will catch that exception here!
695
705
}
696
-
</pre>
706
+
</code>
707
+
</program>
708
+
</listing>
697
709
698
710
<p>
699
711
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.
0 commit comments