0

the code below is printing out 15 15, however I was expecting it to print out 12 15. It seems like the fix method is updating a1 so that it contains 3,7,5 as opposed to 3,4,5. Anyone know why this is the case?

class PassA 
{
    public static void main(String [] args) 
{
    PassA p = new PassA();
    p.start();
}

void start() 
{
    long [] a1 = {3,4,5};
    long [] a2 = fix(a1);
    System.out.print(a1[0] + a1[1] + a1[2] + " ");
    System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3) 
{
    a3[1] = 7;
    return a3;
}
}
Sharingan
  • 333
  • 5
  • 19
  • What would you have happen instead? In the end a2 and a1 point to the same array, which you have modified to have 7 as the middle element. – Kayaman Jan 21 '15 at 10:45
  • As far as you're not performing deep copy of array in the `fix` method, `a1` and `a2` are both referencing array with values `{3,4,5}`. – bsiamionau Jan 21 '15 at 10:48
  • fix() gets as an argument a reference to an array. This means that it change the values of the original array. fix() changes the value of the second cell (remember that array indexes starts from 0) making it {3,7,5} – Noamiko Jan 21 '15 at 10:51

3 Answers3

1

Take a look at following

 long[] fix(long[] a3) { // a3=a1 and a1= {3,4,5}
    a3[1] = 7; // a3[1]=7 means a1[1] will become 7(a1[1]=7), now a1={3,7,5}
    return a3;// return a1
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

This would achieve your target 12 15

long [] fix(long [] a3) 
{
    return new long[]{a3[0], 7, a3[2]};
}

Because otherwise, you pass a1 (named as a3), modify an element, which subsequently changes it in a1. So now a1 is changed. Later on you return a1 and set it to a2.. So a2 and a1 are pointing to the same array {3,7,5}

Amr
  • 2,420
  • 2
  • 17
  • 26
0

You are passing the array by reference, thats why in the fix() method, the original array gets modified. You can pass a copy to the method using

long [] a2 = fix( Arrays.copyOf(a1, a1.length));.

OK, technically, you are passing the array by value, but the value is the reference.

Dave
  • 1,784
  • 2
  • 23
  • 35
  • 1
    You might wanna check this thread, since java is allways pass by value ;) http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – SomeJavaGuy Jan 21 '15 at 10:48
  • Because in Java arrays are object, when passing by value you pass the reference. – Dave Jan 21 '15 at 10:53
  • Ok if wouldn't pass arrays, instead I'd pass a fixed value i.e string or integer, how would this differ? – Sharingan Jan 21 '15 at 11:01
  • Everything in Java gets passed by value, which means that the parameter gets **copied** when entering a method. When passing primitives (byte, short, int, long, float, double, boolean, char) they simply get copied, you have a new primitive inside the method that has the same value as the one passed as a parameter. When passing **anything** else, you are passing an object reference, which in turn gets copied when entering the method. But you are still operating on the original object. This is also why the original array gets modified. – Dave Jan 21 '15 at 11:10