Hello, just interesting b is link to a?

just interesting if we like
do that

int[] a = {1,2,3};
int[] b = a;

b is link to a, or b is {1,2,3}?
and if we redact b (if its link), we redact b or a?

anyways, thanx

1 Like

The question is

After line 2 does b reference the same array as a or does it hold a separate copy of array a.?

If we try this

int [] a = {1, 2, 3};
int [] b = a;
println("--- Array b ---");
printArray(b);
a[0] = 999;  // change first element of array 'a'
println("--- Array b ---");
printArray(b);

we get the output

[0] 1
[1] 2
[2] 3
--- Array b ---
[0] 999
[1] 2
[2] 3

This shows that there is only one array but it is referenced by both a and b

4 Likes

This shows how to make a copy

int [] a = {1, 2, 3};
int [] b = new int[3];
arrayCopy(a,b);
a[0] = 999;
println("--- Array a ---");
printArray(a);
println("--- Array b ---");
printArray(b);

Outputs

--- Array a ---
[0] 999
[1] 2
[2] 3
--- Array b ---
[0] 1
[1] 2
[2] 3
2 Likes

big thanx quark
p.s. i dont needed how to do copy of a