25
Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter 2015 CMPE212 - Prof. McLeod 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

Embed Size (px)

Citation preview

CMPE212 - Prof. McLeod 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 2015

Winter 2015 CMPE212 - Prof. McLeod 2

0

0

0

0

0

0

0

0

0

0

One-Dimensional Arrays - Declaration, Cont.

0180ff

…int[]

testArray

0480ff

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.

Winter 2015 CMPE212 - Prof. McLeod 3

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

0

0

0

0

0

0

0

0

0

00

0

0

0

0

1002fc

1201ab

1010fc

Winter 2015 CMPE212 - Prof. McLeod 4

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.

Winter 2015 CMPE212 - Prof. McLeod 5

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 10twoDArray[0].length // gives 20

Winter 2015 CMPE212 - Prof. McLeod 6

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];

Winter 2015 CMPE212 - Prof. McLeod 7

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); // 4System.out.println(twoDArray[0].length); // 3

Winter 2015 CMPE212 - Prof. McLeod 8

Aliasing Objects - Array Example

1

2

3

4

5

int[]

first

0480ff

0480ff

.length 5

10

20

30

40

50

60

70

int[]

second

0960ff

0960ff

.length 7

int[] first = {1, 2, 3, 4, 5};

int[] second = {10, 20, 30, 40, 50, 60, 70};

Winter 2015 CMPE212 - Prof. McLeod 9

Aliasing Objects - Array Example, Cont.

1

2

3

4

5

int[]

first

0480ff

0480ff

.length 5

10

20

30

40

50

60

70

int[]

second

0480ff

0960ff

.length 7

second = first; // Aliasing!

Winter 2015 CMPE212 - Prof. McLeod 10

Aliasing Objects - Array Example, Cont.

1

2

3

4

5

int[]

first

0480ff

0480ff

.length 5

int[]

second

0480ff

// after garbage collection

Winter 2015 CMPE212 - Prof. McLeod 11

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.

Winter 2015 CMPE212 - Prof. McLeod 12

Aliasing Objects - Array Example, Cont.

1

2

3

4

500

int[]

first

0480ff

0480ff

.length 5

int[]

second

0480ff

first[4] = 500; // second[4] is also 500

Winter 2015 CMPE212 - Prof. McLeod 13

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!

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 2015 CMPE212 - Prof. McLeod 14

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

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?

Winter 2015 CMPE212 - Prof. McLeod 16

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.

Winter 2015 CMPE212 - Prof. McLeod 17

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

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

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?

Winter 2015 CMPE212 - Prof. McLeod 20

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 2015 CMPE212 - Prof. McLeod 21

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 2015 CMPE212 - Prof. McLeod 22

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?

Winter 2015 CMPE212 - Prof. McLeod 23

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

Winter 2015 CMPE212 - Prof. McLeod 24

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 2015 CMPE212 - Prof. McLeod 25