Complete the Arrays as objects exercises before reviewing the solutions.
Review the Arrays as objects exercise 1 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]
mystery1(nums);
System.out.println(Arrays.toString(nums));
}
public static void mystery1(int[] arr)
{
arr[0] = 4;
}
Output
[1, 2, 3]
[4, 2, 3]
Explanation
All arguments in Java are passed by value. In the main
method at the call to mystery1
, the value of nums
is the memory address of the array containing [1, 2, 3]
. The value of arr
is set to a copy of the value of nums
. Both arr
and nums
point to (store the memory address of) the same array.
The statement arr[0] = 4;
changes the value at index 0
in the array to which arr
points.
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
All arrays in Java are objects, regardless of the type of the values inside. nums
stores the memory address of the array (the arrow) just as it would for any other object. The values inside the array are primitive types and are stored directly in the array.
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]
mystery1(nums);
// additional code not yet run
}
public static void mystery1(int[] arr)
{
// additional code not yet run
}
Memory diagram after Step 2
The call to mystery1
passes a copy of the value of nums
, which is memory address of the array (the arrow). nums
and arr
point to (store the memory address of) the same array.
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]
mystery1(nums);
// additional code not yet run
}
public static void mystery1(int[] arr)
{
arr[0] = 4;
}
Memory diagram after Step 3
The statement arr[0] = 4;
changes the value at index 0
in the array to which arr
points. nums
points to the same array.
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]
mystery1(nums);
System.out.println(Arrays.toString(nums));
}
Memory diagram after Step 4
After mystery1
returns (ends), arr
(the parameter) goes away. nums
points to (stores the memory location of) the same array as before the call; however, the value at index 0
is now 4
.
Output after Step 4
[1, 2, 3]
[4, 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[0] = 4;
System.out.println(Arrays.toString(nums));
// prints: [4, 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.
The memory diagrams are the same except that arr
remains in scope until the main
method ends.