When is the value of a variable changed and when is it not?

3 min readdecember 17, 2021


AP Computer Science A 💻

130 resources
See Units

To Change or Not to Change? 🤷‍♂️

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 & Arrays

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.

Primitives

When you pass a primitive (intdoubleboolean) 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, will still be 8 after exiting the method.
public void noChangeInt (int y) {    y = 100; }

Summary 🎉 ✨ 😎

  • To change the values in an object or array, you must go through and change each value
  • Unless you use "return" in your method, you cannot modify the object or array you pass in (for example, you can't make an array hold more values)
  • You cannot change the value of a primitive through a method unless it uses "return"

Browse Study Guides By Unit
Unit 1 – Primitive Types
📱Unit 2 – Using Objects
🖥Unit 3 – Boolean Expressions & if Statements
🕹Unit 4 – Iteration
⚙️Unit 5 – Writing Classes
⌚️Unit 6 – Array
💾Unit 7 – ArrayList
💻Unit 8 – 2D Array
🖲Unit 9 – Inheritance
🖱Unit 10 – Recursion
🙏Exam Reviews

Fiveable
Fiveable
Home
Stay Connected

© 2023 Fiveable Inc. All rights reserved.


© 2023 Fiveable Inc. All rights reserved.