If you fail to close a file, the data might not be written to the file, even if the rest of the program is fine. Actually writing data to a file is the job of the operating system. Your program is only asking the OS to do I/O, but other programs might also be asking for I/O and your requests might have to wait. If your program ends before the I/O is done, the OS might not do it.
If you wanted to keep track of the high temperature for each day for a year,
you might have a Year
object that is composed of 12 Month
objects.
It is convenient to number months starting at one,
so the array would be 13 cells long,
but cell 0 would not be used.
And if you wanted to record the temperatures for a century you might have a Century
object that is composed of 100 Year
objects.
Here is a very rough sketch of a Year
object:
class Year { // instance variables private int year; // year as an int, eg 2017 private Month[] month; public Year ( int year ) { this.year = year; month = new Month[13;] } . . . . }
Notice that each cell of the array month
potentially contains a
reference to a Month
object.
The array might be called an array of Month
s,
but in fact is an array of object references.
This is different from the array in Month
objects where each cell of the array
is a primitive type.
This is a topic of a future chapter.
(Review: ) What are reference variables automatically initialized to?