Reputation: 3943
First of all, I understand what's so called passing by value
in Java.
Also I understand, when you pass an object or an array, it is the array's address that is passed into the method. So modifying the array variable inside the method will affect the outside array variable.
For example,
private void change(int[] a) {
a[0] = 1234;
}
public static void main(String[] args) {
int[] a = new int[2]{1,2};
change(a);
System.out.println(a[0]);
}
The output will be 1234
, because the a
inside change
is actually the array outside.
What I don't understand is the following code:
private void change(int[] a) {
a = new int[3]{1234, 4, 5};
}
public static void main(String[] args) {
int[] a = new int[2]{1,2};
change(a);
System.out.println(a[0]);
}
Why the output is 1
, not 1234
?
The inside a
was the same thing as outside a
, right? I also modify the inside a
just like the example above did, why two different output?
Upvotes: 2
Views: 111
Reputation: 7663
The answer has as much to do with passing by reference or value as it does with which variables are in scope when you attempt to make the change.
When you pass "a," or any variable to a method, you pass the value of that variable - in other words a copy of what it represents- and not the actual variable itself. For example:
public static void main(String[] args){
int a = 3;
addTwo(a);
System.out.println(a);
}
private void addTwo(int a){
a += 2;
}
In the above example the value 3 is passed to addTwo. The variable "a" in the main method is outside of its scope (i.e. it is localized to the main method) so the addTwo method acts only on the copy of "a," the value 3, and not the variable. The variable "a" remains unchanged. When the method completes the value of a is discarded and the original "a" in the main method remains unchanged.
This is the same thing that is happening in your case. It does not matter that you use the same name for two variables. The variable in your main method will not be changed.
Upvotes: 0
Reputation: 616
When you pass a parameter by value, it basically means that it's copied from a method's point of view. So, you have a reference a
to an array, and when you pass it to some method, inside that method you have another reference, also named a
to the same array. Now, when you assign a new value to that reference (a = new int[]...
) you are only modifying the local a
variable to point to a new array.
Upvotes: 0
Reputation: 26846
In Java all variables are references to objects.
When you pass a variable into an array, you pass the reference to the object. If you modify the object then the calling method - which also has a variable that refers to the same object - sees the change.
When you write "a = new int..." you are changing the value of the variable a to point to a different object. It is no longer referring to the same object as the one in the method that called it. So the calling method and the called method see different things.
Upvotes: 0
Reputation: 311028
Let's inspect your function:
private void change(int[] a) {
a = new int[3]{1234, 4, 5};
}
As you stated in your question, a
is a reference to an array. If you use it to modify the array (e.g., a[0]=1234
), you will be changing the same array that was passed in to the function.
However, if you change the reference itself, a will no longer be pointing to the original array, and changes to it will not affect the original.
Upvotes: 1
Reputation: 41271
This is exactly because the reference is passed by value. We'll represent object pointers with 5-digit numbers like 12345
. The process is as follows:
int[]{1,2}
, at memory location, say 10000
. We assign a
the value 10000
. a
is on the stack frame corresponding to main
.change
.change
executes:
change
creates a new array at location 20000 and sets the value of a
on its stack frame to 20000. This does not affect the main
stack frame.change
returns (void).1
.Note that by the time change
returns, we have no live references left to {1234, 4, 5}
.
Upvotes: 0
Reputation: 178243
This is not the same. In your second example, you are changing the local reference a
to refer to a completely new array, not changing the existing array. But this does not change the a
in main to refer to the new array; it still refers to the old array, so 1
is printed.
If you wanted to re-assign a completely new array to the a
in main
using a method, then return the new array from change
and assign that to a
in main
.
Upvotes: 3