190 likes | 358 Views
Today. Quiz 1 solutions posted on the Grading page. Assignment 1 solution posted Wednesday after 7pm. Assignment 2 is posted – due Oct. 24, 7pm. Continue with Pointers in Java: Aliasing pointers. Passing by Reference. Null pointers. Aliasing Objects - Array Example. 5.
E N D
Today • Quiz 1 solutions posted on the Grading page. • Assignment 1 solution posted Wednesday after 7pm. • Assignment 2 is posted – due Oct. 24, 7pm. • Continue with Pointers in Java: • Aliasing pointers. • Passing by Reference. • Null pointers. CISC124 - Prof. McLeod
Aliasing Objects - Array Example 5 int[] first = {1, 2, 3, 4, 5}; int[] second = {10, 20, 30, 40, 50, 60, 70}; 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0960ff .length .length 7 CISC124 - Prof. McLeod
Aliasing Objects - Array Example, Cont. 5 second = first; // Aliasing! 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0480ff .length .length 7 CISC124 - Prof. McLeod
Aliasing Objects - Array Example, Cont. 5 // after garbage collection Poof! 1 2 3 4 5 0480ff int[] first 0480ff int[] second 0480ff .length CISC124 - Prof. McLeod
Aside – “Garbage Collection” in Java • Some computer programming languages require you to indicate when you are done with variables so the memory they are occupying can be released back to the OS. Called “Garbage Collection”. • (Fortunately!) Java has an automatic Garbage Collection system: • Variables are garbage collected once you move outside their scope. • Object contents are garbage collected when there are no pointers pointing to the contents. CISC124 - Prof. McLeod
Aliasing Objects - Array Example, Cont. 5 first[4] = 500; // second[4] is also 500 1 2 3 4 500 0480ff int[] first 0480ff int[] second 0480ff .length CISC124 - Prof. McLeod
Aliasing Objects - Array Example, Cont. • So, setting one array to equal another as in: array1 = array2; sets array1 to point to the same data memory location that was (and still is) pointed to by array2. • Now, changing the value of an element in array2 will change that same element in array1, or visa-versa - this makes sense since both array Objects point to the same set of data values in memory! CISC124 - Prof. McLeod
Aliasing Objects • Passing an Object into a method results in the method’s parameter being aliased to the Object passed. • Called “Passing by Reference”! CISC124 - Prof. McLeod
Passing Parameters by Reference • For example, in main: int[] arrayA = {1, 2, 3, 4, 5}; passArray(arrayA); // invoke passArray • The passArray method: public static void passArray(int[] arrayB) { // arrayB is aliased to arrayA from main // making elemental changes to arrayB will // also change elements in arrayA in main arrayB[3] = 400; } // end passArray // arrayA[3] is 400 in main CISC124 - Prof. McLeod
Passing Parameters by Reference, Cont. • The rule for parameter passing into methods is: • Objects are passed by reference, primitive types are passed by value. • See PassingDemo.java • Has a method with two parameters - an array and an int - which one(s) will stay changed? • Instead of going element by element, if you re-assign the array to another array within the method, what happens? • Does this rule apply to Strings, as well? CISC124 - Prof. McLeod
Passing Arrays by Reference • Summary of PassingDemo.java: • Primitive types are passed by value. • Only element by element changes in arrays will “stick”. • Re-assigning the array to a pointer that has local scope in a method will not “stick”. • If you make element by element changes using an aliased local pointer (like the parameter), changes will “stick”. • Strings are immutable, so this does not apply. You cannot make elemental changes inside a String, even though a String is passed by reference. CISC124 - Prof. McLeod
Passing Arrays by Reference, Cont. • So, mutable Objects (like arrays) can be passed into and out of a method through the parameter list. If a method changes the contents of a mutable Object passed into it – those changes “stick” even when the method is complete. CISC124 - Prof. McLeod
Aside - Comparing Objects • Testing arrays and Objects for equality (with the “==“ boolean operator) is also interesting: • This test will only give a true when both objects have been aliased, using the assignment operator “=“. • So, even if both arrays have identical contents, “==“ will return false, unless both arrays point to the same location. • This means that comparing Objects with “==“ only compares pointers, not contents. CISC124 - Prof. McLeod
Pointers – A Question • So, which way is better to declare a 3 by 10000 two-dimensional array?: int[][] wayOne = new int[3][10000]; int[][] wayTwo = new int[10000][3]; • Or, it makes no difference? CISC124 - Prof. McLeod
How Much Memory Does a Pointer Use? String aString = “Hello!”; • How much memory does aString consume? Not the string itself, just the pointer to the string. • 32 bits for 32 bit Java, 64 bits for 64 bit Java (unless you are using compressed pointers…) • Where is the type information for the pointer contained? (Used by instanceof) CISC124 - Prof. McLeod
null Pointer or null Reference • null is not a keyword in Java – more like a literal constant. • What is a null pointer? • What is a null pointer error? • Does null have a type? • Can you test a pointer to see if it is null? How? • Why would you want to? CISC124 - Prof. McLeod
null Pointer or null Reference, Cont. • What is the difference between string1, string2 and string3 ?: String string1 = ""; String string2 = null; String string3; • See TestNull.java. • Is an unassigned primitive type variable null? • What are the contents of an uninitialized array? CISC124 - Prof. McLeod
null References, Cont. • The idea of a null reference was first introduced into ALGOL W back in 1965 by C.A.R. Hoare (also known as the inventor of Quicksort). • See: http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare CISC124 - Prof. McLeod
Using null in Java • You can test to see if a pointer is null (See file input code, for example. This is how you detect the end of the file.) • Sometimes, to widen the scope of a variable you need to declare it before you can instantiate it. In this case you must assign the variable pointer to null. “Bad things” will happen if you do not! • A NullPointerException is probably the most frustrating error to encounter in Java! CISC124 - Prof. McLeod