Java

Are arrays passed by value or passed by reference in Java duplicate

19 September 2026 · 9 min read

Are arrays passed by value or passed by reference in Java duplicate

Understanding how Java handles data passing is crucial for writing efficient and bug-free code. A common point of confusion arises when dealing with arrays. The question, “Are arrays passed by value or passed by reference in Java?” is frequently asked by developers of all skill levels. The answer, while seemingly straightforward, involves a nuance that’s essential to grasp. Java always passes arguments by value. However, when the variable is a reference to an object, including an array, the value of the reference is passed. This means that the method receives a copy of the reference, not a copy of the array itself. Consequently, changes made to the array’s elements within the method will be reflected in the original array outside the method, because both the original and the method’s copy of the reference point to the same array object in memory. Let’s dive deeper to clarify this important distinction.

Java’s Pass-by-Value Mechanism

Java operates on a pass-by-value principle for all its arguments, regardless of data type. This means that when a method is called, a copy of the variable’s value is passed to the method. For primitive data types like int, float, boolean, etc., the actual value of the variable is copied. Therefore, any modifications made to the parameter inside the method do not affect the original variable outside the method’s scope. This behavior is clear and easily understood.

However, the situation becomes more interesting when dealing with objects, including arrays. In Java, variables of object types do not directly store the object itself. Instead, they store a reference to the object’s location in memory. When an object reference is passed to a method, a copy of this reference is made. Both the original reference and the copied reference now point to the same object in memory. Any changes to the object’s state through either reference will be visible through both. This is why, at first glance, it might seem like arrays are passed by reference, but in reality, it’s the reference that’s passed by value.

To illustrate, consider this analogy: Imagine you have a house (the array object) and two pieces of paper (the references) with the house’s address written on them. One piece of paper is held by you, and the other is given to your friend (the method). If your friend paints the house a different color using their address, you will also see the change when you look at the house using your address. The house itself (the array) wasn’t copied, just the address (the reference).

Arrays as Objects in Java

Arrays in Java are treated as objects. This is a crucial point to remember because it dictates how they behave when passed as arguments to methods. Since arrays are objects, the variables that hold arrays store references to the array’s location in memory. This is unlike primitive data types, where the variable directly holds the value.

When you pass an array to a method, you’re effectively passing a copy of the reference to the array. Both the original reference in the calling code and the copied reference in the method now point to the same array object. This implies that any modification to the array’s elements within the method will directly affect the original array. For instance, if a method modifies an element at a specific index in the array, that change will be reflected when the calling code accesses the same element in its original array.

It’s important to distinguish between modifying the array’s elements and modifying the reference itself. If you reassign the reference within the method to point to a completely new array, the original array reference in the calling code remains unchanged. The method’s reference now points to a different array object, while the original reference still points to the original array. This is because the method only received a copy of the original reference.

Illustrative Code Example

Let’s examine a code example to further clarify how arrays are handled in Java. The following snippet demonstrates how changes to an array within a method affect the original array:

java public class ArrayPassingExample { public static void modifyArray(int[] arr) { arr[0] = 100; // Modifying an element of the array arr = new int[]{1, 2, 3}; // Reassigning the reference (doesn’t affect original array) } public static void main(String[] args) { int[] myArray = {10, 20, 30}; System.out.println(“Before modification: " + myArray[0]); // Output: 10 modifyArray(myArray); System.out.println(“After modification: " + myArray[0]); // Output: 100 } } In this example, the modifyArray method receives a copy of the reference to myArray. Inside the method, the first element of the array is changed to 100. This change is reflected in the main method because both references point to the same array object. However, when the arr reference is reassigned to a new array new int[]{1, 2, 3}, this reassignment does not affect the original myArray reference in the main method. The key takeaway is that changing the contents of the array through the reference affects the original array, but changing the reference itself does not.

This behavior is consistent with Java’s pass-by-value semantics. The value being passed is the memory address (reference) of the array. The method receives a copy of that address. Modifying the data at that address modifies the original array, as both references point there. However, assigning a new address to the method’s copy doesn’t change the original address.

Practical Implications and Considerations

Understanding that arrays are, in effect, passed by reference in Java (more precisely, a reference is passed by value) has several practical implications. It allows you to efficiently modify arrays within methods without needing to return a new array. This can be particularly useful when dealing with large arrays, as it avoids the overhead of creating and copying large amounts of data.

However, it also means you need to be cautious when modifying arrays within methods, as these changes will affect the original array. If you don’t want to modify the original array, you’ll need to create a copy of the array within the method and work with the copy instead. You can use methods like Arrays.copyOf() or System.arraycopy() to create a new array with the same elements as the original.

Here are some key considerations:

  • Be mindful of side effects when modifying arrays within methods.
  • Use array copying techniques when you need to preserve the original array.
  • Understand the difference between modifying array elements and reassigning the array reference.
Infographic here
Here's a quick rundown of how to copy an array in Java:
  1. Use Arrays.copyOf(originalArray, originalArray.length) to create a new array with the same size and elements.
  2. Use System.arraycopy(sourceArray, sourceStartIndex, destinationArray, destinationStartIndex, length) for more fine-grained control over copying.
  3. Manually create a new array and iterate through the original array to copy each element.

For example, int[] newArray = Arrays.copyOf(myArray, myArray.length); creates a completely independent copy of myArray. Modifying newArray will not affect myArray.

Featured Snippet:

In Java, while arguments are always passed by value, when dealing with arrays, the value that’s passed is a reference to the array’s location in memory. This means that changes made to the array elements within a method will be reflected in the original array because both the original and the method’s copy of the reference point to the same array object. However, reassigning the array reference within the method will not affect the original array reference.

  • Java always uses pass-by-value.
  • Arrays are objects, so variables hold references.

FAQ

**Q: Does Java have pass by reference?**
A: No, Java is strictly pass-by-value. However, for objects (including arrays), the value passed is a reference.
**Q: What happens if I reassign the array reference inside a method?**
A: Reassigning the reference inside the method will not affect the original array reference in the calling code. The method's reference will now point to a different array object.
**Q: How can I avoid modifying the original array when passing it to a method?**
A: Create a copy of the array within the method using Arrays.copyOf() or System.arraycopy() and work with the copy.
The concept of "**Are arrays passed by value or passed by reference in Java?**" can be tricky, but understanding that Java is always pass-by-value, and that array variables hold references, is key to mastering this aspect of the language. This knowledge allows you to write more predictable, maintainable, and efficient code. For additional reading, consider exploring resources on Java memory management and object-oriented programming principles. Learning about deep copies versus shallow copies is also very useful. You can also learn from sites like Baeldung [Baeldung on Pass by Value vs. Pass by Reference in Java](https://www.baeldung.com/java-pass-by-value-vs-pass-by-reference), and GeeksforGeeks [GeeksforGeeks on Java is Pass-by-Value or Pass-by-Reference](https://www.geeksforgeeks.org/java-is-pass-by-value-or-pass-by-reference/).

Understanding how data is handled in Java is the foundation for developing robust, scalable applications. By grasping the nuances of pass-by-value with object references, you can avoid common pitfalls and write more reliable code. Don’t let this concept intimidate you; practice with code examples and experiment with different scenarios. And remember, the official Java documentation Oracle’s Java Tutorials are always a great resource. Keep exploring, keep learning, and you’ll find yourself becoming a more confident and capable Java developer. If you found this information valuable, share it with your fellow developers! And if you’re looking to take your Java skills to the next level, explore our advanced Java courses and discover how you can build enterprise-grade applications. Check out our related article on Java memory management.

Question & Answer :

Arrays are not a [primitive type](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) in Java, but they [are not objects either](https://stackoverflow.com/questions/5564423/arrays-in-java-and-how-they-are-stored-in-memory), so are they passed by value or by reference? Does it depend on what the array contains, for example references or a primitive type?

Everything in Java is passed by value. In case of an array (which is nothing but an Object), the array reference is passed by value (just like an object reference is passed by value).

When you pass an array to other method, actually the reference to that array is copied.

  • Any changes in the content of array through that reference will affect the original array.
  • But changing the reference to point to a new array will not change the existing reference in original method.

See this post: Is Java “pass-by-reference” or “pass-by-value”?

See this working example:

public static void changeContent(int[] arr) { // If we change the content of arr. arr[0] = 10; // Will change the content of array in main() } public static void changeRef(int[] arr) { // If we change the reference arr = new int[2]; // Will not change the array in main() arr[0] = 15; } public static void main(String[] args) { int [] arr = new int[2]; arr[0] = 4; arr[1] = 5; changeContent(arr); System.out.println(arr[0]); // Will print 10.. changeRef(arr); System.out.println(arr[0]); // Will still print 10.. // Change the reference doesn't reflect change here.. }