AP Computer Science A
3 min read•Last Updated on July 11, 2024
Milo Chang
Milo Chang
Sometimes, you pass a variable into a method but its value is not changed. Other times, you pass in a variable and its value is changed. In this article, we'll help you understand how to tell which situation you are looking at.
Make sure you check out this guide of all the best resources for APCSA to prepare for your exam.
Objects and arrays are passed into a method by reference (Strings in Java are objects). A reference tells the computer where the values of the object or array are stored; a reference is like an address that tells the computer where to find the values.
The reference cannot be changed by the method; it is immutable.
You can change the contents of the object or array, you just cannot create or change it. You can change what values are stored, but you cannot change how many are stored or change its location to another address.
Suppose there is an array of integers called testArray which contains the values {0,1,2,3,4,5}.
Passing this array into the following code will change its values: ✔
public void changeArray (int[] testArray)
{
for (int i=0; i<testArray.length; i++)
{
testArray[i] += 1;
}
}
After this method runs, the values of testArray will be {1,2,3,4,5,6}. This is because the method takes the reference, accesses the location of the values of testArray, and changes the values that are stored.
Let's reset testArray back to {0,1,2,3,4,5}.
On the other hand, this code will not change the values of the array: 🚫
public void noChangeArray (int[] testArray)
{
int[] second = {5,2,4,2,3,1};
testArray = second;
}
After this method runs, the values of testArray will still be {0,1,2,3,4,5}. This is because when you create array second, you create a new array that is stored at a different address than testArray. By using "testArray = second", you are setting the address of testArray to the address of second. While the computer will print {5,2,4,3,4,1} inside of this method, once you exit the method, the original reference of testArray will be restored and the values of testArray will once again be {0,1,2,3,4,5}.
To change the values stored inside an object or array, you must go through and change each value, not try to change its address. 👀
If you do want to change the actual object or array (like making the array bigger), your method has to use "return". You create a new object or array inside your method and "return" that once the method is called. The returned object or array must be stored into the variable.
When you pass a primitive (int, double, boolean) into a method, you are just passing a copy of its value into the method. You are not passing the original primitive into the method. This means that you cannot the value of a primitive unless your method uses "return" and the returned value is stored into the variable.
If you pass the integer y = 8 into this method, y will still be 8 after exiting the method.
public void noChangeInt (int y)
{
y = 100;
}