Array Passed By Value or Passed By Reference in Java

This tutorial introduces an array passed by value or reference in Java.
When we call a method, its arguments can be passed as value or as a reference. Before discussing both these, let us understand two terms:
- callee: This is the method called by another method
- caller: This is the method that calls another method
Let us now learn what pass by value and pass by reference means.
What is Passed By Value in Java
When we say that the arguments are passed by value, a copy of the actual parameter’s value is made in memory.
The caller and callee maintain two independent variables with the same value. If the callee modifies the parameter value, we cannot see the change in the caller.
Important points:
- Callee does not have access to the original element in the calling code.
- A copy of the data (parameter values) is sent to the callee.
- Changes to the passed variable do not affect the final value.
What is Passed By Reference in Java
When we say that the arguments are passed by reference (also called pass by address), it means passing the reference of an argument in the calling function to the corresponding formal parameter.
A copy of the actual argument’s address is made in this situation. The caller and the callee use the same variable for the parameter, and therefore, if the callee modifies the parameter variable, we can see the change in the caller’s variable.
Important points:
- Callee gives a reference to the programming element in the calling code.
- The stored data’s memory address is passed instead of the value.
- Any changes to the value will affect the original data
Java is Passed By Value
Unlike C++, in Java, we can pass arguments only as values. But there’s a catch: when objects are passed as arguments, their reference is passed as value to the callee.
In other words, even though Java is strictly passed by value, it behaves as passed by reference for objects. Let’s use some examples to understand this concept better.
Passing Primitive Data Types in Java
In the code below, we pass two integers as the arguments to a function, and then we change their values.
Since arguments are passed by value in Java, the function maintains a separate copy of the arguments. Any change that we make to the arguments is made in function copy only and not in the copy that the main function (caller) maintains.
Therefore, we get the original values when we print the values after the function call is finished. Look at the example code below:
public class SimpleTesting {
static void JE_function(int n1, int n2){
n1 = -232;
n2 = -90;
}
public static void main(String args[]) {
int JE_numa = 6932;
int JE_numb = 8934;
System.out.println("Numbers before function call :" +JE_numa + " and " +JE_numb);
JE_function(JE_numa, JE_numb);
System.out.println("Numbers after the JE_function call :" +JE_numa + " and " +JE_numb);
}
}
Output:
Numbers before function call:6932 and 8934
Numbers after the JE_function call:6932 and 8934
Passing Array in Java
When it comes to objects, the reference to the object is passed by value as the argument.
In the code below, we pass an Array as an argument, and when we print the value of the object (array), a memory location gets printed. This has to do with the fact that an object variable stores the location of the memory where the object is stored.
Look at the example code below:
public class SimpleTesting {
static void JE_function(String[] sentence){
System.out.println(sentence);
}
public static void main(String args[]) {
String[] sent = {"I", "Love", "Delftstack"};
JE_function(sent);
}
}
Output:
[Ljava.lang.String;@442d9b6e
Using the above code, we try to convey a reference to the array passed as the argument. Thus, if we change the value of the array (not the memory location), the change will be reflected in the callee.
Look at the example code below:
public class SimpleTesting {
static void JE_function(String[] sentence){
sentence[1] = "Visit";
}
public static void main(String args[]) {
String[] sent = {"I", "Love", "Delftstack"};
System.out.println("Sentence before calling the function");
for( String word: sent){
System.out.println(word);
}
JE_function(sent);
System.out.println("Sentence after calling the function");
for( String word: sent){
System.out.println(word);
}
}
}
Output:
Sentence before calling the function
I
Love
Delftstack
Sentence after calling the function
I
Visit
Delftstack
As seen from the above code, any change to the array is reflected in the main block.
Think of it as if two friends are working on a project. Any work one of the friends does will be reflected in the overall project.
The function, in this case, refers to the same array that the main block uses.
Related Article - Java Array
- Concatenate Two Arrays in Java
- Convert Byte Array in Hex String in Java
- Remove Duplicates From Array in Java
- Count Repeated Elements in an Array in Java
- Natural Ordering in Java
- Slice an Array in Java