This question already has an answer here:
-
Is Java “pass-by-reference” or “pass-by-value”?
78 answers
I have a question about changing the values of variables in methods in Java.
This is my code:
public class Test {
public static void funk(int a, int[] b) {
b[0] = b[0] * 2;
a = b[0] + 5;
}
public static void main(String[] args) {
int bird = 10;
int[] tiger = {7};
Test.funk(bird, tiger);
}
}
After the execution of the method Test.funk(bird, tiger)
, the value of bird is not changed - it remains with the value 10
, even though in the funk()
method we have changed the value with a = b[0] + 5;
On the other hand, the value of the element in the array changes, because we have the statement b[0] = b[0] * 2;
I don't understand why one thing changes and the other not? Could someone please explain this for me.
Look at Jon Skeet's article about Parameter-Passing in Java, which explains this.
In short (look at his site for a more throughout explanation):
Arrays are reference types. If you pass a reference that points to an array, the value of the reference is copied and assigned to the parameter of the function. So the parameter will point to the same array as the argument that was passed. Thus changes you make to the array through the parameter of your function will be visible in the calling function. Changing the parameter itself (b), for example by setting it to null, however, will not be noticed by the calling function, since the parameter (b) is just a copy of the argument (tiger) passed.
Integers are so-called primitive types. Passing the integer copies its value and assigns it to the parameter too. But that value is not a reference to the actual data, but is the data itself. So changes to the paramter in the function will affect the parameter (a), but not the argument passed in the calling function (bird).
Basically, objects (like arrays) are passed into methods "by reference". So when you change the object, it changes the same object that was passed into the method.
Primitives (like int) are "passed by value", so the variable you are assigning a value to in a is not the same as the int variable that was passed in.
I hope this helps...
That's because when you declare
public static void funk(int a, int[] b)
The scope of the variable a is only that method. Then when you change the value, you are changing only the value of that variable in that method.
About the b. Thats a new object reference to the same array created in main, that's why it seems the value does change ( what is changing is the array object underneath )
But try this:
public static void funk(int a, int[] b) {
// create a new reference for b
int[] c = new int[b.length];
c[0] = b[0];
b = c;
// The same.
b[0] = b[0] * 2;
a = b[0] + 5;
}
When you do that you'll the the value of tiger didn't change either ( only the content of the new array c created in funk )
You can simulate the pass by ref using a wrapper. See this post.
Although I haven't got any comments about that
EDIT Just for fun:
I have modified your code to use the wrapper posted above.
It looks pretty strange but looks like it works.
// By ref simulated.
public class Test {
public static void funk(_<Integer> a, int[] b) {
b[0] = b[0] * 2;
a.s( b[0] + 5 ) ;
}
public static void main(String[] args) {
_<Integer> bird = new _<Integer>(10);
int[] tiger = {7};
Test.funk( bird , tiger );
System.out.println("bird = " + bird );
System.out.println("tiger = " + tiger[0] );
}
}
Prints
bird = 19
tiger = 14
:-S
One variable is passing by reference and the other is by value :)
What's the difference between passing by reference vs. passing by value?