Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.

Similar presentations

Presentation on theme: "Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter."— Presentation transcript:

1 Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter 2015CMPE212 - Prof. McLeod1

2 Winter 2015CMPE212 - Prof. McLeod One-Dimensional Arrays - Declaration, Cont. 0180ff … int[] testArray 0480ff 10.length As a “pointer”, testArray points to an area of memory that contains a series of int values as well as the attribute length.

3 Winter 2015CMPE212 - Prof. McLeod3 Multi-Dimensional Arrays Consider: int[][] exArray = new int[3][5]; int[][] exArray 0480ff int[] exArray[0] 1002fc int[] exArray[1] 1010fc int[] exArray[2] 1201ab fc 1201ab 1010fc

4 Winter 2015CMPE212 - Prof. McLeod4 Multi-Dimensional Arrays - Cont. So exArray points to three one dimensional arrays: exArray[0] exArray[1] exArray[2] Each of these arrays has the same length: exArray[2].length // returns 5 Yes, you can refer to these arrays in code, just like this.

5 Winter 2015CMPE212 - Prof. McLeod5 Multi-Dimensional Arrays - Cont. int[][] twoDArray = new int[10][20]; The above is equivalent to: int[][] twoDArray; twoDArray = new int[10][]; for (int i = 0; i < 10; i++) twoDArray[i] = new int[20]; As shown above: twoDArray.length// gives 10 twoDArray[0].length // gives 20

6 Winter 2015CMPE212 - Prof. McLeod6 Multi-Dimensional Arrays - Cont. “Ragged Arrays” are not “square”, and are legal in Java: int[][] raggedArray = new int[5][]; raggedArray[0] = new int[5]; raggedArray[1] = new int[3]; raggedArray[2] = new int[2]; raggedArray[3] = new int[8]; raggedArray[4] = new int[12];

7 Winter 2015CMPE212 - Prof. McLeod7 Multi-Dimensional Arrays - Cont. Array initializer for two-D array, for example: int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; System.out.println(twoDArray.length); // 4 System.out.println(twoDArray[0].length); // 3

8 Winter 2015CMPE212 - Prof. McLeod8 Aliasing Objects - Array Example int[] first 0480ff.length int[] second 0960ff.length 7 int[] first = {1, 2, 3, 4, 5}; int[] second = {10, 20, 30, 40, 50, 60, 70};

9 Winter 2015CMPE212 - Prof. McLeod9 Aliasing Objects - Array Example, Cont int[] first 0480ff.length int[] second 0480ff 0960ff.length 7 second = first; // Aliasing!

10 Winter 2015CMPE212 - Prof. McLeod10 Aliasing Objects - Array Example, Cont int[] first 0480ff.length 5 int[] second 0480ff // after garbage collection

11 Winter 2015CMPE212 - Prof. McLeod11 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.

12 Winter 2015CMPE212 - Prof. McLeod12 Aliasing Objects - Array Example, Cont int[] first 0480ff.length 5 int[] second 0480ff first[4] = 500; // second[4] is also 500

13 Winter 2015CMPE212 - Prof. McLeod13 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!

14 Aliasing Objects Passing an Object into a method results in the method’s parameter being aliased to the Object passed. Called “Passing by Reference”! Winter 2015CMPE212 - Prof. McLeod14

15 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 Winter 2015CMPE212 - Prof. McLeod15

16 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 –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? Winter 2015CMPE212 - Prof. McLeod16

17 Passing Arrays by Reference Summary of –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. Winter 2015CMPE212 - Prof. McLeod17

18 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. Winter 2015CMPE212 - Prof. McLeod18

19 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. Winter 2015CMPE212 - Prof. McLeod19

20 Pointers – A Question So, which way is better to declare a 3 by two-dimensional array?: int[][] wayOne = new int[3][10000]; int[][] wayTwo = new int[10000][3]; Or, it makes no difference? Winter 2015CMPE212 - Prof. McLeod20

21 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…) Winter 2015CMPE212 - Prof. McLeod21

22 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? Winter 2015CMPE212 - Prof. McLeod22

23 null Pointer or null Reference, Cont. What is the difference between string1, string2 and string3 ?: String string1 = ""; String string2 = null; String string3; See Is an unassigned primitive type variable null ? What are the contents of an uninitialized array? Winter 2015CMPE212 - Prof. McLeod23

24 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: References-The-Billion-Dollar-Mistake-Tony-Hoare Winter 2015CMPE212 - Prof. McLeod24

25 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! Winter 2015CMPE212 - Prof. McLeod25

Download ppt "Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter."

Similar presentations

Ads by Google