Summary of Class Presentation for June 17, 1999 Topic: cleanup, initialization We started by examining the finalize method. Java does not have the C++ notion of destructors. Instead, the finalize method is automatically called whenever the object has its storage released by the garbage collector. Unfortunately, the finalize method might not ever be called since an object might never be released by the garbage collector. Also, the garbage collector will run at unpredictable times that depend on the particular way it is implemented ( this is not specified by the Java language). Further, whether or not a specific object will actually be released by the garbage collector depends on the specific algorithm of the garbage collector. You do know, however, that no object will ever be released by the garbage collector, if there are any handles to that object still accessible. Since when and even whether the finalize method is ever called cannot be predicted by the programmer, finalize methods are rarely used. Some uses include: 1. closing a stream to allow its use by another thread; 2. closing a network connection; 3. starting another thread of execution. We also looked at initialization. Variables are defined in the order they appear in a class. A variable can be initialized, therefore, with a value that depends on the current value of a variable that was initialized earlier in the class. For example, the following is valid in Java: int a = 3; float b = (float) a + 2; Variables that are not explicitly initialized have values automatically set by the compiler to the following: int, byte, short, long 0 float, double 0.0 char '' boolean false Objects are automatically initialized when the storage is set up by the use of the new operator. If the constructor does not directly set the values of any data field, that field is initialized to the same default values as for variables of the same type. Array initialization can be done by listing a set of possible values within curly brackets. For example, long arr[ ] = { 14, 18, 12, 7, 9, 12, 3, 0, -2, 11}; Multiple dimensional arrays are initialized using a set of values within brackets for each rightmost dimension, nested within curly brackets for each dimension moving to the left. Here, for example, is an initialization of a two-dimensional and then a three-dimensional array: double tarr = { {3.4, 2.3, 3, 7.3, -4.1}, {2.33, 3.456, -2, 4,3, 7.1234} } int qarr = { { {2, 3, 4, 5,6}, {1, 2, 3, 4, 5}, {2, 3, 4, 5,6} }, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 1}, {3, 3, 3, 3, 3} } }, { 6, 5, 4, 3, 2}, {5, 6, 7, 8, 9}, {1, 2, 3, 4, 5} }, {6, 5, 4, 3, 21}, {2, 4, 6, 8, 120}, {11, 13, 15, 17, 3}}};