Complete the Arrays as objects exercises before reviewing the solutions.
Review the Arrays as objects exercise 2 solution with AP CS Tutor Brandon Horn.
Original code
public static void main(String[] args)
{
int[] nums = new int[] {1, 2, 3};
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
mystery2(nums);
System.out.println(Arrays.toString(nums));
}
public static void mystery2(int[] arr)
{
arr = new int[3];
arr[0] = 5;
}
Output
[1, 2, 3]
[1, 2, 3]
Explanation
As in the Exercise 1 solution, nums
and arr
point to (store the memory location of) the same array. The statement arr = new int[3];
changes the actual value of arr
, which has no effect on the value of nums
.
See the bottom of this page for similar code without a method call.
Step by step memory diagram
Step 1
public static void main(String[] args)
{
int[] nums = new int[] {1, 2, 3};
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
// additional code not yet run
}
Memory diagram after Step 1
This is the same as in Exercise 1.
Output after Step 1
[1, 2, 3]
Step 2
public static void main(String[] args)
{
int[] nums = new int[] {1, 2, 3};
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
mystery2(nums);
// additional code not yet run
}
public static void mystery2(int[] arr)
{
// additional code not yet run
}
Memory diagram after Step 2
This is also the same as in Exercise 1.
Output after Step 2
[1, 2, 3]
Step 3
public static void main(String[] args)
{
int[] nums = new int[] {1, 2, 3};
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
mystery2(nums);
// additional code not yet run
}
public static void mystery2(int[] arr)
{
arr = new int[3];
// additional code not yet run
}
Memory diagram after Step 3
The statement ... = new int[3];
creates a new array containing [0, 0, 0]
. The statement arr = ...;
sets the value of arr
to the memory address of the new array.
All arguments in Java are passed by value. Changing the actual value of the parameter arr
has no effect on the value of nums
.
Output after Step 3
[1, 2, 3]
Step 4
public static void main(String[] args)
{
int[] nums = new int[] {1, 2, 3};
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
mystery2(nums);
// additional code not yet run
}
public static void mystery2(int[] arr)
{
arr = new int[3];
arr[0] = 5;
}
Memory diagram after Step 4
Output after Step 4
[1, 2, 3]
Step 5
public static void main(String[] args)
{
int[] nums = new int[] {1, 2, 3};
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
mystery2(nums);
System.out.println(Arrays.toString(nums));
}
Memory diagram after Step 5
When mystery2
returns (ends), arr
(the parameter) goes away. Since nothing points to the array containing [5, 0, 0]
, it goes away too. nums
points to the original array, that still contains the orginal values [1, 2, 3]
.
Output after Step 5
[1, 2, 3]
[1, 2, 3]
Similar code without method call
public static void main(String[] args)
{
int[] nums = new int[] {1, 2, 3};
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
int[] arr = nums;
arr = new int[3];
arr[0] = 5;
System.out.println(Arrays.toString(nums));
// prints: [1, 2, 3]
}
This code is nearly the same as the original code.
The statement int[] arr = nums;
creates a new variable arr
that points to (stores the memory address of) the array to which nums
points. This is the same as in Exercise 1.
The statement arr = new int[3];
creates a new array and points arr
at the new array.
Since arr
and nums
no longer point to the same array, the statement arr[0] = 5;
has no effect on the array to which nums
points.
The memory diagrams are the same except that arr
remains in scope, and the array to which arr
points continues to exist, until the main
method ends.