Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing array in method changes array outside [duplicate]

Tags:

I have trouble with scope of variable.

public static void main(String[] args){     int[] test={1,2,3};     test(test);     System.out.println(test[0]+" "+test[1]+" "+test[2]); }  static void test(int[] test){     test[0]=5; } 

I expected the output to 1 2 3, but the result was 5 2 3. Why I changed the value in the array in the method, but the original array changed?

like image 495
purblue Avatar asked Feb 08 '14 23:02

purblue


People also ask

Can you change an array in a method?

In this way, the contents of an array CAN be changed inside of a method, since we are dealing directly with the actual array and not with a copy of the array. Note: In Java, a pointer value to the memory address location is used, making arrays "pass-by-value" (and not actually "pass-by-reference" as seen in C++.)

Why does changing an array in JavaScript affect copies of the array?

An array in JavaScript is also an object and variables only hold a reference to an object, not the object itself. Thus both variables have a reference to the same object.

What happens when you modify an array inside of a function Java?

So, when you modify the array in your method via that reference, you're modifying the single array object that exists on the heap. You commented that you made a "copy" of the array via int[] temp=test ... again, this only makes a copy of the reference value (pointer) that points to the single array in memory.

How do you pass an entire array to a method in Java?

Passing Array To The Method In Java To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);


2 Answers

An array in Java is an object. When you create an array via new, it's created on the heap and a reference value (analogous to a pointer in C) is returned and assigned to your variable.

In C, this would be expressed as:

int *array = malloc(10 * sizeof(int)); 

When you pass that variable to a method, you're passing the reference value which is assigned (copied) to the local (stack) variable in the method. The contents of the array aren't being copied, only the reference value. Again, just like passing a pointer to a function in C.

So, when you modify the array in your method via that reference, you're modifying the single array object that exists on the heap.

You commented that you made a "copy" of the array via int[] temp=test ... again, this only makes a copy of the reference value (pointer) that points to the single array in memory. You now have three variables all holding the same reference to the same array (one in your main(), two in your method).

If you want to make a copy of the array's contents, Java provides a static method in the Arrays class:

int[] newArray = Arrays.copyOf(test, test.length);  

This allocates a new array object on the heap (of the size specified by the second argument), copies the contents of your existing array to it, then returns the reference to that new array to you.

like image 97
Brian Roach Avatar answered Sep 21 '22 17:09

Brian Roach


Definitions:

  • Reference = A variable that points to the location in memory where your array lives.
  • Value of a Reference = The actual memory address location itself

You passed the value of the reference of your array into your test() method. Since java is Pass By Value, it passes the value of the reference, not the value of your array (ie. a copy).

It may be easier to think of a reference as a pointer if you have a C background. So a value of a reference is essentially it's memory address (I'm fudging java rules here but it may be simplest to think of it this way)

So, in your example, you pass the value of the reference which points to your array, into your test() method, which then uses that reference value to lookup where your array is in memory, so it can access data in your array.

Since in your test() method you do not change your array's reference (where it points to, ie. test = new int[10];), then your test() method acts on the original data in the array (because it still points to your original array's location), leading to element 0 being set to a value of 5.

like image 38
SnakeDoc Avatar answered Sep 19 '22 17:09

SnakeDoc