-1

Consider the following code:

public static void resize(int[] x){
x = new int[x.length*2];
System.out.println(x.length + " ");
}

public static void main(String[] args){
int[] x = {1,2};
resize(x);
System.out.println(x.length);
}

The output is "4 2". The question is: I thought that when we are defining an array in the code of the new length, the other array (the previous one with length 2) would be discarded, as now the value of the array points to the "larger" array. So, why would then print out at the end as length 2? I used Arrays.toString to verify, and indeed, the actual values of the array after the void method are {1,2}. This is confusing, as I thought that the array itself would be changed as the value is a pointer to the memory address (in contrast with using the method on a char/int variable, which would not affect the value of the variable).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Guysont
  • 111
  • 4
  • 1
    Repeat after me; Java always passes by value, Java always passes by value, Java always passes by value, Java always passes by value, Java always passes by value .... – Peter Lawrey Apr 23 '14 at 16:46

3 Answers3

1

When you call resize, you pass an array object to the method. This is the basic flow of your program:

initialize array of size 2
pass that array to resize()
  resize has a reference to the value of the array
  resize points it's reference to a new array twice the size of the old reference
  prints "4"
main() prints the size of the initial array "2"

You don't change the original array in the new method, it merely has an array of the same value.

Rogue
  • 11,105
  • 5
  • 45
  • 71
  • Then how would we explain the fact that if we set `x[0]` and `x[1]` to be other values (without defining a new array) and then print it at the end, we get the "modified values"? Or would we have that by when setting the equality of x to a larger array, we will have essentially created a "new array" with a different reference? (and then, eliminated by garbage collection) Then, if we have a method as a return value being the new array (and setting it equal to x), we will then have a new array as the variable x points specifically to this newer array? – Guysont Apr 23 '14 at 17:21
  • Try following the link that was provided in regards to this being a duplicate question, it answers all of that :) – Rogue Apr 23 '14 at 17:26
0

Because Java passes by value, the array doesn't change after your call to resize. (I'm assuming the output is actually 4 2, not 2 4.)

If you want resize to effect a permanent change, you should have the method return x; as its final statement (or use a global variable):

public static int[] resize(int[] x){
x = new int[x.length*2];
System.out.println(x.length + " ");
return x;
}

public static void main(String[] args){
int[] x = {1,2};
x = resize(x);
System.out.println(x.length);
}
Kayvon
  • 3
  • 2
0

The scope of your x variable in resize

public static void resize(int[] x){
   x = new int[x.length*2];
}

is local to that function. It does not affect the x that was passed in. This means that as soon as resize completes, that local copy disappears and is eventually garbage collected.

If you want to resize the original array, return it. For example:

public static int[] getNewArrayOfDoubleLength(int orig_length){
   return  new int[orig_length * 2];
}

And call it with

x = getNewArrayOfDoubleLength(x.length);

Since arrays are immutable, this is a new array. The original one still exists (although it's inaccessible) until it's garbage collected.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108