More arrays

54
More arrays More arrays Primitive vs. reference Primitive vs. reference parameters. parameters. Arrays as parameters to Arrays as parameters to functions. functions.

description

More arrays. Primitive vs. reference parameters. Arrays as parameters to functions. Recall from our room reservation problem how we initialized our hotel array. final int N = 100; //# of rooms //note: there are N rooms which are subscripted 0..N-1. boolean rooms[] = new boolean [ N ]; - PowerPoint PPT Presentation

Transcript of More arrays

Page 1: More arrays

More arraysMore arrays

Primitive vs. reference Primitive vs. reference parameters.parameters.

Arrays as parameters to functions.Arrays as parameters to functions.

Page 2: More arrays

Recall from our room reservation problem Recall from our room reservation problem how we initialized our hotel array.how we initialized our hotel array.

final intfinal int N = 100; //# of roomsN = 100; //# of rooms//note: there are N rooms which are subscripted 0..N-1.//note: there are N rooms which are subscripted 0..N-1.booleanboolean rooms[] = new boolean[ N ];rooms[] = new boolean[ N ];

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<N; i++) {for (int i=0; i<N; i++) {

rooms[i] = false;rooms[i] = false;}}

Can we put this code in a function that we call from our Can we put this code in a function that we call from our main function?main function?Can we use arrays as function parameters?Can we use arrays as function parameters?

Page 3: More arrays

Warning!Warning!

TechnicalTechnical

definitionsdefinitions

ahead.ahead.

Page 4: More arrays

Java typesJava types

““There are two kinds of types in the Java There are two kinds of types in the Java programming language:programming language:

1.1. primitive types andprimitive types and

2.2. reference types.”reference types.”

from from http://docs.oracle.com/javase/specs/jls/se7/jlshttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdf7.pdf

Page 5: More arrays

Java valuesJava values

““There are, correspondingly, two kinds of data There are, correspondingly, two kinds of data values that can be stored in variables, passed as values that can be stored in variables, passed as arguments, returned by methods, and operated arguments, returned by methods, and operated on:on:

1.1. primitive values andprimitive values and

2.2. reference values.”reference values.”

from from http://docs.oracle.com/javase/specs/jls/se7/jls7.phttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdfdf

Page 6: More arrays

Primitive typesPrimitive types

booleanbooleanbytebyteshortshort intint longlongcharchar floatfloatdoubledouble

Page 7: More arrays

Reference typesReference types

““There are four kinds of reference types: class There are four kinds of reference types: class types, interface types, type variables, and types, interface types, type variables, and array types.”array types.”

from from http://docs.oracle.com/javase/specs/jls/se7/jlshttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdf7.pdf

Page 8: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j;int j;

int[] A;int[] A; // A and B are references// A and B are references

Int[] B;Int[] B;

Page 9: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

Page 10: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

Page 11: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----10

Page 12: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

Page 13: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

Page 14: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------

-------

-------…-------

Page 15: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------5-------

-------…-------

Page 16: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------5-------

-------…-------

B-----

.

Page 17: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------5 9-------

-------…-------

B-----

.X

Page 18: More arrays

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] ); 99

i-----10

j-----

10 5X

A-----

.

-------5 9-------

-------…-------

B-----

.X

Page 19: More arrays

Recall from our room reservation problem Recall from our room reservation problem how we initialized our hotel array.how we initialized our hotel array.

final intfinal int N = 100; //# of roomsN = 100; //# of roomsbooleanboolean rooms[] = new boolean[ N ];rooms[] = new boolean[ N ];//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<N; i++) {for (int i=0; i<N; i++) {

rooms[i] = false;rooms[i] = false;}}

Can we put this code in a function that we call from Can we put this code in a function that we call from our main function?our main function? Yes!Yes!Can we use arrays as function parameters?Can we use arrays as function parameters?

Yes!Yes!

Page 20: More arrays

Our hotel reservation system.Our hotel reservation system.

class Hotel {class Hotel {static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {//How do we use this new function from main?//How do we use this new function from main?

}}}}

Page 21: More arrays

Our hotel reservation system.Our hotel reservation system.

class Hotel {class Hotel {static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

//How do we use initializeHotel from main?//How do we use initializeHotel from main?

}}}}

Page 22: More arrays

Our hotel reservation system.Our hotel reservation system.

class Hotel {class Hotel {static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

initializeHotel( rooms );initializeHotel( rooms );

}}}}

Page 23: More arrays

Our hotel reservation system.Our hotel reservation system.class Hotel {class Hotel {

//init our hotel to all empty//init our hotel to all emptystatic void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

initializeHotel( rooms );initializeHotel( rooms );

}}}}

This is an example of a parameter with a reference value. Changes made in initializeHotel to occupied array entries change rooms array entries in main.

Page 24: More arrays

Our hotel reservation system.Our hotel reservation system.

How about another function to find the first How about another function to find the first free (available/unoccupied) room?free (available/unoccupied) room? It should return the room # of the first available It should return the room # of the first available

room.room.

If no rooms are available, it should return -1.If no rooms are available, it should return -1.

We will also specify a range of room numbers to We will also specify a range of room numbers to check (we won’t always check the entire hotel).check (we won’t always check the entire hotel).

Page 25: More arrays

So far we have…So far we have…

class Hotel {class Hotel {

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

Page 26: More arrays

So far we have…So far we have…

class Hotel {class Hotel {

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

Page 27: More arrays

Our hotel reservation system:Our hotel reservation system:first free roomfirst free room

//This function returns the number of the first available//This function returns the number of the first available// in the range of [first,last] inclusive.// in the range of [first,last] inclusive.// If no room can be found, -1 is returned.// If no room can be found, -1 is returned.static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

int found = -1;int found = -1;for (int i=first; i<=last; i++) {for (int i=first; i<=last; i++) {

//find the first room that is not occupied//find the first room that is not occupiedif ( !occupied[i] ) {if ( !occupied[i] ) {

found = i;found = i; //remember the free room #//remember the free room #break;break; //terminate the loop//terminate the loop

}}}}return found;return found;

}}

Page 28: More arrays

How can we use this new function in How can we use this new function in main to check is a room between # main to check is a room between #

10 and # 20 is available?10 and # 20 is available?

Page 29: More arrays

How can we use this new function in main to How can we use this new function in main to check is a room between # 10 and # 20 is check is a room between # 10 and # 20 is

available?available?public static void main ( String args[] ) {public static void main ( String args[] ) {

final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

initializeHotel( rooms );initializeHotel( rooms );//check for a room between 10 and 20//check for a room between 10 and 20int rm = firstFree( 10, 20, rooms );int rm = firstFree( 10, 20, rooms );System.out.println( "room # " + rm + " is available" System.out.println( "room # " + rm + " is available"

););}} In the call to firstFree, 10 and 20 are

primitive values, and rooms is a reference value.

Page 30: More arrays

How can we use this new function in How can we use this new function in main to check our entire hotel for the main to check our entire hotel for the

first free room?first free room?

Page 31: More arrays

How can we use this new function in main to How can we use this new function in main to check our entire hotel?check our entire hotel?

//check for a room in the entire hotel//check for a room in the entire hotel

int r = firstFree( 0, N-1, rooms );int r = firstFree( 0, N-1, rooms );

System.out.println( "room # " + rSystem.out.println( "room # " + r

+ " is available" );+ " is available" );In this call to firstFree, 0 and N-1 are primitive values, and rooms is a reference value.

Page 32: More arrays

Passed by value vs. passed by Passed by value vs. passed by referencereference

So what’s the difference?So what’s the difference?

For a For a primitive valueprimitive value, any changes to the , any changes to the variable in the function DO NOT affect the variable in the function DO NOT affect the caller.caller.

For a For a reference valuereference value, any changes in the , any changes in the function to the data to which the reference function to the data to which the reference refers DO affect the caller.refers DO affect the caller.

Page 33: More arrays

Passed by value vs. passed by Passed by value vs. passed by reference example.reference example.

static void test ( int A, int B[] ) {static void test ( int A, int B[] ) {A = A + 12;A = A + 12;B[0] = B[0] + 12;B[0] = B[0] + 12;

}}……In main:In main:

int A = 5;int A = 5;

int D[] = new int [2];int D[] = new int [2];D[0] = 5;D[0] = 5;D[1] = 5;D[1] = 5;

test( A, D );test( A, D );System.out.println( "A is " + A + “, D[0] is " + D[0] );System.out.println( "A is " + A + “, D[0] is " + D[0] );

What’s the output?

Remember, A is a primitive value, and D is a reference value.

Page 34: More arrays

Passed by value vs. passed by Passed by value vs. passed by reference example.reference example.

static void test ( int A, int B[] ) {static void test ( int A, int B[] ) {A = A + 12;A = A + 12;B[0] = B[0] + 12;B[0] = B[0] + 12;

}}……In main:In main:

int A = 5;int A = 5;

int D[] = new int [2];int D[] = new int [2];D[0] = 5;D[0] = 5;D[1] = 5;D[1] = 5;

test( A, D );test( A, D );System.out.println( "A is " + A + “, D[0] is " + D[0] );System.out.println( "A is " + A + “, D[0] is " + D[0] );

What’s the output?

A is 5, D[0] is 17

test made changes to both A and B[0] but since A is a primitive value, those changes don’t affect A in main.

Since B is a reference value, the changes made by test to B[0] do affect D[0] in main.

Page 35: More arrays

Back to our hotel reservation Back to our hotel reservation system…system…

Write a function that, given a room number Write a function that, given a room number and our rooms array, indicates that that and our rooms array, indicates that that room is occupied.room is occupied.

Page 36: More arrays

So far we have…So far we have…

class Hotel {class Hotel {

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

Page 37: More arrays

So far we have…So far we have…class Hotel {class Hotel {

static void bookARoom ( int rm, boolean rooms[] ) {static void bookARoom ( int rm, boolean rooms[] ) {

……

}}

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

Page 38: More arrays

Back to our hotel reservation Back to our hotel reservation system…system…

Write a function that, given a room number and Write a function that, given a room number and our rooms array, indicates that that room is our rooms array, indicates that that room is occupied.occupied.

static void bookARoom ( int rm, boolean rooms[] )static void bookARoom ( int rm, boolean rooms[] )

{{

… … ////what do we need to do here?what do we need to do here?

}}

Page 39: More arrays

Back to our hotel reservation Back to our hotel reservation system…system…

Write a function that, given a room number and Write a function that, given a room number and our rooms array, indicates that that room is our rooms array, indicates that that room is occupied.occupied.

static void bookARoom ( int rm, boolean rooms[] )static void bookARoom ( int rm, boolean rooms[] )

{{

rooms[ rm ] = true;rooms[ rm ] = true;

}}

Page 40: More arrays

Back to our hotel reservation Back to our hotel reservation system…system…

//given a room number and our rooms array,//given a room number and our rooms array,// indicate that that room is occupied.// indicate that that room is occupied.static void bookARoom ( int rm, boolean rooms[] )static void bookARoom ( int rm, boolean rooms[] ){{

//it would be wise to check rm here first to determine if//it would be wise to check rm here first to determine if// it is between 1..# of rooms.// it is between 1..# of rooms.rooms[ rm ] = true;rooms[ rm ] = true;

}}……In main:In main:

bookARoom( 1, rooms );bookARoom( 1, rooms );bookARoom( 2, rooms );bookARoom( 2, rooms );bookARoom( 10, rooms );bookARoom( 10, rooms );

Page 41: More arrays

More hotel reservation system…More hotel reservation system…

Wouldn’t it be useful to know how many Wouldn’t it be useful to know how many rooms are occupied in our hotel?rooms are occupied in our hotel?

We can write function to do that!We can write function to do that!

Page 42: More arrays

So far we have…So far we have…class Hotel {class Hotel {

static void bookARoom ( int rm, boolean rooms[] ) {static void bookARoom ( int rm, boolean rooms[] ) {

……

}}

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

static int occupiedCount ( boolean rooms[] ) {static int occupiedCount ( boolean rooms[] ) {

……

}}

}}

Page 43: More arrays

More hotel reservation system…More hotel reservation system…

//function that determines how many rooms//function that determines how many rooms// are occupied in our hotel// are occupied in our hotelstatic int occupiedCount ( boolean rooms[] )static int occupiedCount ( boolean rooms[] ){{

int count = 0;int count = 0;for (int i=0; i<rooms.length; i++) {for (int i=0; i<rooms.length; i++) {

//check here needed.//check here needed.}}

}}

Page 44: More arrays

More hotel reservation system…More hotel reservation system…

//function that determines how many rooms//function that determines how many rooms// are occupied in our hotel// are occupied in our hotelstatic int occupiedCount ( boolean rooms[] )static int occupiedCount ( boolean rooms[] ){{

int count = 0;int count = 0;for (int i=0; i<rooms.length; i++) {for (int i=0; i<rooms.length; i++) {

if ( rooms[i] )if ( rooms[i] )++count;++count;

}}return count;return count;

}}

Page 45: More arrays

More hotel reservation system…More hotel reservation system…

//function that determines how many rooms//function that determines how many rooms// are occupied in our hotel// are occupied in our hotelstatic int occupiedCount ( boolean rooms[] )static int occupiedCount ( boolean rooms[] ){{

int count = 0;int count = 0;for (int i=0; i<rooms.length; i++) {for (int i=0; i<rooms.length; i++) {

if ( rooms[i] )if ( rooms[i] )++count;++count;

}}return count;return count;

}}

Now, how can we use this new function from main?Now, how can we use this new function from main?

Page 46: More arrays

Room in use.Room in use.

In main:In main:

bookARoom( 1, rooms );bookARoom( 1, rooms );

bookARoom( 2, rooms );bookARoom( 2, rooms );

bookARoom( 10, rooms );bookARoom( 10, rooms );

System.out.println( occupiedCount(rooms)System.out.println( occupiedCount(rooms)

+ " in use." );+ " in use." );

Page 47: More arrays

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function that returns the percentage of Write a function that returns the percentage of free rooms in the hotel. (Your function should free rooms in the hotel. (Your function should call occupiedCount.)call occupiedCount.)

Page 48: More arrays

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function to allow someone to check Write a function to allow someone to check out. Given a room number, this function out. Given a room number, this function should indicate that the room is no longer should indicate that the room is no longer occupied.occupied.

Page 49: More arrays

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function to find a block (i.e., Write a function to find a block (i.e., contiguous) of 3 rooms.contiguous) of 3 rooms.

Return the room # of the first room in the block.Return the room # of the first room in the block.If no such block is available, return -1.If no such block is available, return -1.

Page 50: More arrays

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function called findARoom that finds an Write a function called findARoom that finds an available room.available room.

But we don’t want to simply use firstFree because we’ll But we don’t want to simply use firstFree because we’ll wear out some rooms in our hotel and not use other wear out some rooms in our hotel and not use other rooms at all.rooms at all.

So findARoom should generate a random number and So findARoom should generate a random number and see if that room is free. If it’s not free, findARoom can see if that room is free. If it’s not free, findARoom can then use firstFree.then use firstFree.

Page 51: More arrays

RECAPRECAP

Page 52: More arrays

Java typesJava types

““There are two kinds of types in the Java There are two kinds of types in the Java programming language:programming language:

1.1. primitive types andprimitive types and

2.2. reference types.”reference types.”

from from http://docs.oracle.com/javase/specs/jls/se7/jlshttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdf7.pdf

Page 53: More arrays

Java valuesJava values

““There are, correspondingly, two kinds of data There are, correspondingly, two kinds of data values that can be stored in variables, passed as values that can be stored in variables, passed as arguments, returned by methods, and operated arguments, returned by methods, and operated on:on:

1.1. primitive values andprimitive values and

2.2. reference values.”reference values.”

from from http://docs.oracle.com/javase/specs/jls/se7/jls7.phttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdfdf

Page 54: More arrays

Function parameters, and Function parameters, and primitive and reference values.primitive and reference values.

Changes to primitives do not affect the Changes to primitives do not affect the calling function.calling function.

Changes to references (more specifically, Changes to references (more specifically, changes to what the reference refers) do changes to what the reference refers) do affect the calling function. affect the calling function.