0

if this is a shallow copy

double[] a = new double[100];
a = b; // let b be some other double array[100]

I understand that the better way to do this is using a for loop or using

System.arrayCopy(b,0,a,0,100);

However what happens to this?

public double[] function1(){
    returns somedouble[100];
}

double[] a = new double[100];
a = function1(); // i believe this will also be a shallow copy

System.arrayCopy(function1(),0,a,0,100); // will this call function1 100 times?
2
  • a = b is not a shallow copy. Its just changing the a pointer to point to the start of b. Commented Jun 4, 2016 at 6:19
  • If it's primitive array, then there's no difference between shallow and deep copy. First, you need to understand primitive/reference type in Java. Then you learn primitive/reference arrays. Then you know there's "shallow or deep copy" about reference array. Commented Jun 4, 2016 at 6:35

2 Answers 2

2
double[] a = new double[100];
a = b; // let b be some other double array[100]

Create an array named a of double with size of 100. Now when you a = b will copy the reference of b array to variable a.

     +--------------------------------------------+  <- Suppose whole
a->  | 2.5 |  |  |  |  |  |  |  |  |  |  |  |  |  |  array is filled 
     +--------------------------------------------+  with value 2.5

     +--------------------------------------------+  <- Suppose whole
b->  | 7.9 |  |  |  |  |  |  |  |  |  |  |  |  |  |  array is filled 
     +--------------------------------------------+  with value 7.9

after a = b

     +--------------------------------------------+  <- Suppose whole
     | 2.5 |  |  |  |  |  |  |  |  |  |  |  |  |  |  array is filled 
     +--------------------------------------------+  with value 2.5

a->  +--------------------------------------------+  <- Suppose whole
b->  | 7.9 |  |  |  |  |  |  |  |  |  |  |  |  |  |  array is filled 
     +--------------------------------------------+  with value 7.9

So now a and b pointing to same array.

public double[] function1(){
    return somedouble[100];
}

double[] a = new double[100];
a = function1();

Now same thing is happening here. You create an array named a and then call function1() and again assigned the returning array reference to a.

System.arraycopy(function1(), 0, a, 0, 100);

Here the calling order will be

1 -> function1() will be called and the returning array reference will be saved in a temporary variable.

2 -> call to System.arraycopy(temporary variable, 0, a, 0, 100)

So function1() will be called only once.

As a side note make sure you use System.arraycopy(args) instead of System.arrayCopy(args)

Sign up to request clarification or add additional context in comments.

Comments

0
    double[] a = new double[100];
    a = b; // let b be some other double array[100]

First, It's assigning, not copying.


    double[] a = new double[100];
    a = function1(); // i believe this will also be a shallow copy

It's assigning. you assign the returned value somedouble[100] to a


System.arrayCopy(function1(),0,a,0,100); // will this call function1 100 times?

No, it won't call function1 100 times. The above code mostly equals to

    double[] tmpref = function1();
    System.arrayCopy(tmparr,0,a,0,100);

Because it first evaluates the arguments, then it calls arrayCopy

1 Comment

Thanks for the clarification. I am not sure who downvoted it, but your answer was definitely helpful to me. Upvote to you sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.