开发者

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

This question already has answers here: Is Java "pass-by-reference" or "pass-by-value"? (92 answers) Closed 6 years ago.

I thought almost all languages, including java, pass array into function as reference (modifiable).

But somehow it does not work here, and the testArray is still 1,2,3 with size of 3.

Strange enough, when if I change result[i] = 2 to a[1] =2 it works. It did pass by reference.

What is wrong with this code?

At the end, I had a = result; (which update the a). Did result get removed from stack. Is that why I still get to the original a?

I am confused.

Thanks!

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

      int[] testArray = {1,2,3};
      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 void 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;
   }
}


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.


Arrays are Objects in java. If you have initialized your array with a size(actually length), you cannot modify it. You can create a new Array with a varying size (as you are currently doing in the function ) and copy all the values from the Old Array to the new Array. In your case, you have 2 objects and 3 references. If you want to access the Array of greater size in the calling function , make the function return the reference of the Array of greater size.


public class test
{
    public static void main(String[] args){
        int[] a = {15, 2, -3};
        printArray(a);
        changeArray(a);
        printArray(a);
    }
    private static void changeArray(int[] a){
        for(int i = 0; i < a.length; i++){
            a[i]++;
        }
    }

    private static void printArray(int[] a){
        for(int i = 0; i < a.length; i++){
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}

-----OUTPUT-----

15 2 -3

16 3 -2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜