CSE110
Principles of Programming
with Java
Lecture 24:
References, Arrays of Objects, and Parameters
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
References
JavierGonzalez-Sanchez|CSE110|Summer2017|3
Assignment Revisited
• The act of assignment takes a copy of a value and
stores it in a variable
• For primitive types:
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
Reference Assignment
• An object reference variable holds the memory
address of an object
• Rather than dealing with arbitrary addresses, we
often depict a reference graphically as a “pointer”
to an object
• For object references, assignment copies the
memory location:
jane john jane john
JavierGonzalez-Sanchez|CSE110|Summer2017|5
Reference Assignment
Arrays of Objects
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
Arrays of Objects
• The elements of an array can be object references.
• Suppose that we have the Student class
Student[] list = new Student[5];
//The following causes NullPointerException
int id = list[0].getID();
//We need to instantiate each object first:
list[0] = new Student(“Bob”);
list[1] = new Student(“Mary”);
list[2] = new Student(“Peter”);
// ...
// OR we can use the Initializer:
Student[] list = {new Student(“Bob”),
new Student(“Mary”),
new Student(“Peter”) };
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
Arrays of Objects
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
Arrays of Objects
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Arrays of Objects
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11
Arrays of Objects
Objects as Parameters
JavierGonzalez-Sanchez|CSE110|Summer2017|13
Objects as Parameters
• Parameters in a Java method are passed by value
• This means that a copy of the actual parameter
(the value passed in) is stored into the formal
parameter (in the method header)
• Passing parameters is therefore similar to an
assignment statement
• When an object is passed as a parameter to a
method, the actual parameter and the formal
parameter become aliases of each other
JavierGonzalez-Sanchez|CSE110|Summer2017|14
Objects as Parameters
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15
Objects as Parameters
JavierGonzalez-Sanchez|CSE110|Summer2017|16
Objects as Parameters
• Since a1 is a primitive data, any change that was made
to f1 in the method changeValues does not affects a1
• The object a2 was assigned to the value “222” and
when a2 was passed to changeValues method, a2 and
f2 became aliases so the change made to f2 will be
seen in a2 in the main method.
• The object a3 was assigned to the value “333” and
when a3 was passed to changeValues method, a3 and
f3 became aliases.
However when f3 was re-instantiated with the reserved
word new, f3 is given a different address from the one a3
has, thus any change made to f3 after that point will not
affect a3 in the main method.
JavierGonzalez-Sanchez|CSE110|Summer2017|17
Reference
Chapter 6
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

201707 CSE110 Lecture 24