-1

I have an IntArray A.

int[] A={1,2,3};

and I copy it to B.

int[] B=A;

now I want to change element in A;

A[1]=B[2];

I would expect A now becomes {1,3,3} where B stays the same {1,2,3}.

Why is B changed to [1,3,3]?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
NewbieDave
  • 1,199
  • 3
  • 12
  • 23
  • Here's how you can do it: http://stackoverflow.com/questions/5785745/make-copy-of-array-java – kiheru Sep 14 '13 at 09:12

6 Answers6

3

They both are not individual arrays.

You wrote

int[] B=A;

Because there are pointing to the same array.

If you change A automatically B changes.

If you want to make B constant create a new array in the name of B.

int[] B = new int[3];

As you confused with pass by reference and value :Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
3

Because you are not copying the array, you are simple working with references.

This mean that if you create an array you are assigning a piece of memory to that variable.

int[] array ----------------> PieceOfMemory

If you "copy" the array as you did you are simply telling another variable to point to the same piece of memory

int[] array ----------------> PieceOfMemory <----------------- int[] secondArray

In this case if you change "PieceOfMemory" is clear that you are changing both of the variables.

If you want to copy the array you have to do like so:

int[] firstArray = {1,2,3};
int[] secondArray = new int[3];
secondArray = (int[]) clone(firstArray);

now what happens is this:

int[] firstArray ---------------------> firstPieceOfMemory
int[] secondArray --------------------> secondPieceOfMemory

But the values are copied.

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Gianmarco
  • 2,536
  • 25
  • 57
  • Appreciate it. Now I do understand Java is totally pass by reference. Do I use clone to force it pass by value? Does clone works for other classes? Thanks. – NewbieDave Sep 14 '13 at 09:29
  • I suggest you to check the javadoc and Java documentation, it's the only way to know "www": what works where. Some classes uses clone, some other don't, you can even override a default clone method or create one for your POJO (plain old java object). I also suggest you to have a look to this question http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Gianmarco Sep 14 '13 at 09:33
  • 2
    @NewbieDave Java is **NOT** pass by reference! It is always pass by value, but the value is the reference and not the referenced object. – Fabian Barney Sep 14 '13 at 09:37
  • @Gianmarco by this `secondArray = (int[]) clone(firstArray);` did you meant `secondArray = firstArray.clone();` ? – exexzian Dec 07 '13 at 21:52
2

When you do

int[] B=A;

B and A both point to same backing array. B and A here are just references pointing to same underlying array.

A ----->[1,2,3]
           ^
B----------|

To make a new copy you can use Arrays#copyOf or manually create a new array and copy fields one by one.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
2

You need to understand the difference between objects and references.

I.e. B=A; does not make a copy of the data, but a copy of the reference to the data.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • Is everything in Java passed by reference? not value? so no pointer in Java? – NewbieDave Sep 14 '13 at 09:23
  • @NewbieDave Yes.For Objects you can say pass by reference,primitives are pass by value.Again yes there is no pointers in java.Simple and clean :) – Suresh Atta Sep 14 '13 at 09:29
  • 1
    @NewbieDave No, Java is always passed by value. But the value is the reference, not the data. You always pass a reference copy to the same object. – Fabian Barney Sep 14 '13 at 09:29
  • @NewbieDave As fabian said ,That reference can be primitive value that holds the adress of that reference. – Suresh Atta Sep 14 '13 at 09:31
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Actually, everything except primitives are pointers. The actual limitation is that you are not allowed to compute pointers yourself. – Has QUIT--Anony-Mousse Sep 16 '13 at 07:34
1

Because B isn't a new array, it's just another name for the array that A refers to.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Yes B also changed to {1,3,3} because A and B are just references actual memory where the array is stored are same for both the array so both array contains same value.

Satyam Koyani
  • 4,236
  • 2
  • 22
  • 48