Article #4 What does it mean when we say Java is pass by value ?
In Java, when we pass a variable as an argument to a method, the value of that variable is copied and passed to the method rather than the variable itself. This means that the method receives a copy of the original value, and any changes made to the parameter within the method do not affect the original variable.
This behavior is referred to as "pass by value" because the method receives a copy of the variable's value, not the variable itself.
For example, consider the following code:
public class Example {
public static void main(String[] args) {
int num = 10;
System.out.println("Before calling the method, num = " + num);
changeNum(num);
System.out.println("After calling the method, num = " + num);
}
public static void changeNum(int num) {
num = 20;
System.out.println("Inside the method, num = " + num);
}
}
In this code, we define a variable num
with an initial value of 10
. We then call the method changeNum
and pass num
as an argument.
Inside the changeNum
method, we assign the value 20
to the parameter num
. However, this change does not affect the original variable num
outside the method. When we print the value of num
after calling the method, we still get the output 10
.
This is because num
was passed by value to the method changeNum
, and the parameter num
only contains a copy of the original value 10
. The method can change the value of the parameter num
, but this does not affect the original variable num
in the main
method.
In Java, every variable is either a primitive type or a reference type.
Primitive types, such as int
, float
, boolean
, etc., hold the actual value of the data they represent. When a primitive type variable is passed as an argument to a method, the value of the variable is copied and passed to the method. This means that any changes made to the parameter inside the method do not affect the original variable.
Reference types, on the other hand, hold a reference (i.e., memory address) to an object in the heap. When a reference type variable is passed as an argument to a method, the reference (i.e., memory address) of the object is copied and passed to the method. This means that the parameter variable holds a copy of the reference to the object, but both the parameter variable and the original variable refer to the same object in memory.
So, when a method modifies the state of an object that is passed as an argument, the changes are reflected in the original object as well, since both the parameter variable and the original variable refer to the same object. However, if the method assigns a new object to the parameter variable, the original variable is not affected because the parameter variable now refers to a new object.
Let's take another example to illustrate the concept of pass by value with reference types:
csharpCopy codepublic class Example {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println("Before calling the method, arr[0] = " + arr[0]);
changeArray(arr);
System.out.println("After calling the method, arr[0] = " + arr[0]);
}
public static void changeArray(int[] arr) {
arr[0] = 10;
System.out.println("Inside the method, arr[0] = " + arr[0]);
}
}
In this example, arr
is a reference type variable that refers to an array of integers. When we pass arr
as an argument to the changeArray
method, the reference (i.e., memory address) of the array is copied and passed to the method. Inside the changeArray
method, we modify the value of the first element of the array to 10
. This change is reflected in the original array as well because both arr
and the parameter arr
refer to the same array in memory.
I hope this helps to clarify the concept of "pass by value" in Java.