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
+32-16Lines changed: 32 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -524,11 +524,11 @@ void main() {
524
524
<title>List</title>
525
525
526
526
<p>
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.
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. <xrefref="python-histogram"text="type-global"/> is a simple Python program that creates and prints a histogram.
Lets review what is happening in this little program. First, we create a list and initialize the first 10 positions in the list to be 0. Next we open the data file called ‘test.dat’. Third, we have a loop that reads each line of the file. As we read each line we convert it to an integer and increment the counter at the position in the list indicated by the number on the line we just read. Finally we iterate over each element in the list, printing out both the position in the list and the total value stored in that position.
560
566
</p>
561
567
562
568
<p>
563
-
To write the Java version of this program we will have to introduce several new Java concepts. First, you will see the Java equivalent of a list, called an <c>ArrayList.</c> Next, you will see three different kinds of loops used in Java. Two of the loops we will use are going to be very familiar, the third one is different from what you are used to in Python but is easy when you understand the syntax:
569
+
To write the Java version of this program we will have to introduce several new Java concepts. First, you will see the Java equivalent of a list, an <c>ArrayList</c>. Next, you will see three different kinds of loops used in Java. Two of the loops we will use are going to be very familiar, the third one is different from what you are used to in Python but is easy when you understand the syntax:
564
570
</p>
565
571
566
572
<p>
@@ -616,7 +622,7 @@ The <c>code</c> will be executed once for each element in the <c>collection</c>.
616
622
</p>
617
623
618
624
<p>
619
-
Here is the Java code needed to write the exact same program:
625
+
<xrefref="java-histogram"text="type-global"/> is the Java code needed to write the exact same program.
620
626
</p>
621
627
622
628
<note>
@@ -625,7 +631,8 @@ Here is the Java code needed to write the exact same program:
Before going any further, I suggest you try to compile the above program and run it on some test data that you create.
674
+
Before going any further, I suggest you try to compile <xrefref="java-histogram"text="type-global"/> and run it on some test data that you create.
667
675
</p>
668
676
669
677
<p>
670
678
Now, let’s look at what is happening in the Java source. As usual, we declare the variables we are going to use at the beginning of the method. In this example we are declaring a <c>Scanner</c> variable called <c>data</c>, an integer called <c>idx</c> and an <c>ArrayList</c> called <c>count</c>. However, there is a new twist to the <c>ArrayList</c> declaration. Unlike Python where lists can contain just about anything, in Java we let the compiler know what kind of objects our array list is going to contain. In this case the <c>ArrayList</c> will contain <c>Integers</c>. The syntax we use to declare what kind of object the list will contain is the <c><Type></c> syntax.
671
679
</p>
672
680
673
681
<p>
674
-
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:
682
+
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 <xrefref="java-arraylist-warning"text="type-global"/> when you compile the program.
675
683
</p>
676
684
677
-
<pre>
685
+
<listingxml:id="java-arraylist-warning">
686
+
<program>
687
+
<code>
678
688
Note: Histo.java uses unchecked or unsafe operations.
679
689
Note: Recompile with -Xlint:unchecked for details.
680
-
</pre>
690
+
</code>
691
+
</program>
692
+
</listing>
681
693
682
694
<p>
683
695
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)
684
696
</p>
685
697
686
698
<p>
687
-
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.
699
+
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. <xrefref="java-try-catch"text="type-global"/> shows the general structure of a try/catch block.
688
700
</p>
689
-
690
-
<pre>
701
+
702
+
<listingxml:id="java-try-catch">
703
+
<program>
704
+
<code>
691
705
try {
692
706
Put some risky code in here, like opening a file
693
707
} catch (Exception e) {
694
708
If an error happens in the try block an exception is thrown. We will catch that exception here!
695
709
}
696
-
</pre>
710
+
</code>
711
+
</program>
712
+
</listing>
697
713
698
714
<p>
699
715
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