Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java array pass by reference does not work? [duplicate]

Tags:

java

People also ask

Do arrays get passed by reference in Java?

Arrays are Objects, yes, but nothing in Java is passed by reference. All parameter passing is by value. In the case of an Object, what gets passed is a reference to the Object (i.e. a pointer), by value. Passing a reference by value is not the same as pass by reference.

Is double passed by reference Java?

Nothing in Java is passed by reference. Everything is passed by value.

Does Java pass by copy?

You've already learned that Java variables are passed by value, meaning that a copy of the value is passed. Just remember that the copied value points to a real object in the Java memory heap. Passing by value still changes the value of the real object.


The array is passed by reference, but the reference is passed by value. That is, you can change the array that a refers to, but you cannot change which array a refers to.


Java is pass by value. This is why your code does not work. A good practice would be to mark int[] a as final so this would result in a compilation error (see the corresponding Checkstyle rule).


return parameter "a" from the function and assign to testArray in the main function. When you pass an object by reference, the reference is copied and given to the function. So the object is now referenced by 2 references. Any changes in the object through the 2nd reference will reflect in the first reference, because it is the same object referenced by both of them. But when you change the reference (not the object through reference), it is a different case. you have changed the 2nd reference to point to another object(int[] result). So any changes through the 2nd reference will change only the "result" object.

class Test
{
   public static void main(String[] args)
   {

      int[] testArray = {1,2,3};
      testArray = equalize(testArray, 6);

      System.out.println("test Array size :" + testArray.length);
      for(int i = 0; i < testArray.length; i++)
         System.out.println(testArray[i]);
   }

   public static int[] equalize(int[] a, int biggerSize)
   {
      if(a.length > biggerSize)
         throw new Error("Array size bigger than biggerSize");

      int[] result = new int[biggerSize];
     // System.arraycopy(a, 0, result, 0, a.length);
     // int array default value should be 0
      for(int i = 0; i < biggerSize; i++)
         result[i] = 2;

      a = result;
      return a;
   }
}

When you do a = result; object a dosnt anymore point to the testArray, bc you are changing its reference to result's address. That's why it dosnt effect anymore to the testArray. What you are doing actually is you are making a the same adress as result has, so whatever you change in a it will change in result too.

Hope this helped...


The array referenced by a can be modified, but the reference itself is passed by value. So if you did a[0] = 1, then you would be changing the original array. However, a = result changes the reference, and so the original reference is unchanged.


Java is pass by value, always.