0

Why is there a difference between the following two programs A & B. Shouldn't they run identical? For some reason the changer in the case of the array is changing the original value of the input array.

Program A:

public static void changer(int tester) {
    tester = tester*2;
}

public static void main() {

    int value = 1;
    out.println(value);
    changer(value);
    out.println(value);     
}

which gives me the output:

1
1

Program B:

public static void changer(int[] tester) {
    tester[0] = tester[0]*2;
}

public static void main(){

        int[] value = {1};
        out.println(value[0]);
        changer(value);
        out.println(value[0]);


}

which gives me the output:

1
2
  • 2
    Related http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Marco13 Oct 04 '15 at 22:46
  • So I understand what is happening, but is there a good reason that these two programs have to behave differently. I mean shouldn't the compiler be written such that both cases are treated the same? – user2154610 Oct 04 '15 at 22:50
  • `int[]` is an _object_ whereas `int` is a _primitive_. That's why the two methods treat modifications to the input parameters differently. That's just how Java works. – Mick Mnemonic Oct 04 '15 at 22:59
  • @user2154610 The 2 cases should be never be treated the same, even for c++. Imagine how inefficient it is to copy the entire array's value every time you passes an array to a method. Take a look at my solution below. – user3437460 Oct 04 '15 at 23:09

5 Answers5

1

Changing a value in a called method does not change the value in the calling method. In Program B, you're not changing the array, you're changing a value inside the array, and that is visible in the calling method.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
1

In your first example, you passed an int (tester) to the function, and then assigned a new value to it. Since java is pass-by-value, assignment has not effect outside of the callee scope, and so the original value was not changed in the call site.

In the second example, you passed an int array to the function, and then you did not assign to it, but modeified its content, by assigning to a specific cell inside of it, so the value was changed in the call site as well.

MByD
  • 135,866
  • 28
  • 264
  • 277
1

Passing primitive data type ( i.e int, double, long, float, ....) to a function or method will be " PASSED BY VALUE". In program A, the argument passed to method is a primitive data (int), so you just passed a copy of the origin value. Any change to it, will not affect the origin number.

But passing Reference/Object data type (i.e, Array, ArrayList, HashMap,...) to a function will be " PASSED OBJECT BY VALUE," which means the method is given copy of the reference to the object. So any change to it, will change the origin reference. In program B, you passed a reference copy of an array. That is why it has been modified.

0

When arrays work like pointers. When you use

tester[0] = tester[0]*2;

You are basically telling compiler to update element in memory on location tester[0]

michnovka
  • 2,880
  • 3
  • 26
  • 58
0

This question had been asked so many times. Java passes everything by value and this is especially true for primitive such as int.

When you pass an int argument, you are passing in the value, not the reference of the variable.

public static void main(String[] args){
    int value= 5;
    changer(val);
}
public static void changer (int tester){
    //A variable call tester holding the value of 5
}

A local variable call tester with value of 5 will be created, because you passed in a value of 5. Variable val itself was not passed in.

Any changes done within the method is merely changing the local variable tester. This explains why the value of val remains unchanged.


When you pass in an array, it still passes by value, but the value does not contain all the values of individual array element. It will be very inefficient to copy the entire array's value for every method invocation. The value holds the reference of the array. Thus, tester now holds the reference of the val.

public static void main(String[] args){
    int[] val= 5;
    changer(val);
}
public static void changer (int[] tester){
    //A variable call tester holding the reference of val array.
}

Since tester is now pointing at the original array: val. Anything you changed in the method will affect the original array.

user3437460
  • 17,253
  • 15
  • 58
  • 106