0

Java is basically a pass-by-value language. But I wrote some code, and found that in that case Java actually passed the reference of an array rather than just its value, because when I changed the values in the array in another method of another class, the original array was changed.

What I did was I created a constructor and passed an array as the argument of that constructor. That constructor then called a method which did some operations on the said array. But, when I checked for the values in that array in my main() method, from which the constructor was called, I found that the values were changed.

If Java was supposed so pass a copy of the array, this shouldn't have happened. Can anyone please help?

Here is the code I'm talking about. The actual code is different but I wrote an example of what I am talking about for the sake of simplicity.

 public class Test
 {
     public static void main(String[] args)
     {
         int[] matrix = {1, 2, 3, 4};
         dump(matrix);

         Changer changer = new Changer(matrix);

        dump(matrix);
    }



    static void dump(int[] matrix)
    {
        for(int i = 0; i<matrix.length; i++)
        {
            System.out.print(matrix[i]);
        }

        System.out.println();
    }
}

class Changer
{

    Changer(int[] matrix)
    {
        change(matrix);
    }

    void change(int[] matrix)
    {
        for(int i = 0; i < 4; i++)
        {
            matrix[i] += 1;
        }
    }
}

Any help is appreciated

EDIT:

Because this question was marked as duplicate, I want to clarify.

I accept that Java is pass-by-value. But I pointed to a situation where the value was not passed, and the reference/original variable was passed. I want some confirmation about Java's behavior

Pratyush Yadav
  • 271
  • 1
  • 13
  • Also http://javadude.com/articles/passbyvalue.htm. – Aleksandr M Sep 15 '15 at 09:24
  • If you want to give the constructor a copy of the array, just use `Arrays.copyOf()`. But keep in mind that if you modify the objects contained in the array, the modifications will leak in the caller's array, because afaict `Arrays.copyOf()` returns a 'shallow' copy, not a 'deep' one. – francesco foresti Sep 15 '15 at 09:33
  • I know that, but I was curious as to why the changes in the array were reflected back in the original array, considering that Java passes value rather than reference, so a duplicate of that array must be passed to that constructor. But the original array is actually passed – Pratyush Yadav Sep 15 '15 at 09:38
  • If you read my answer, it's clarified in just one sentence – Luigi Cortese Sep 15 '15 at 09:46
  • It's explained in the first paragraph of the accepted answer i the marked duplicate: A reference to the object is passed by value, the object is modified via the reference. – peterchen Sep 15 '15 at 10:14
  • Yeah, sorry. Didn't read properly. Got it now. Thanks. – Pratyush Yadav Sep 15 '15 at 10:46

1 Answers1

0

You're actually passing a value, the value of the reference. What does the reference contains? Something like "the address" of the location memory of the array (it's not exactly like that, but you can imagine it like that).

So, you're always pointing at (and modifying) the same object.

Working with primitive datatypes, anyway, shows the behaviour you were expecting:

public static void main(String[] args) {
    int[] matrix = { 1, 2, 3, 4 };
    int value=5;

    System.out.println(matrix);
    System.out.println(Arrays.toString(matrix));
    System.out.println(value);

    foo(matrix);
    bar(value);
    System.out.println();

    System.out.println(matrix);
    System.out.println(Arrays.toString(matrix));
    System.out.println(value);
}

static void foo(int[]matrix){
    matrix[0]=matrix[1]=matrix[2]=matrix[3]=0;
}

static void bar(int value){
    value = 10;
}

outputs

[I@659e0bfd
[1, 2, 3, 4]
5

[I@659e0bfd    //the value of the reference is not modified
[0, 0, 0, 0]   //the content of the matrix is modified
5              //the primitive value is not modified  
Luigi Cortese
  • 9,451
  • 5
  • 33
  • 47