Question

I'm learning deep copy and shallow copy.

If we have two arrays:

int[]arr1={1,2,3,4,5};
int[]arr2={1,2,3,4,5};

Question: Both arrays point to the same references [1][2][3][4][5].

What will happen if I change arr1[2]?Does it changes arr2[2]?

When we pass an array (random array, not necessarily arr1 or arr2) from main into a method the compiler makes a shallow copy of the array to the method, right?

If the Java compiler was re-written to make and send a deep copy of the array data to the method. What happens to the original array in the main? I think the original array may change according to the method and pass back into the main. I am not sure.

Was it helpful?

Solution

Question: Both arrays point to the same references [1][2][3][4][5].

Not exactly. Your arrays have identical content. That being said, as per your initialization, they data they are using is not shared. Initializing it like so would however:

int[]arr1={1,2,3,4,5};
int[] arr2 = arr1

What will happen if I change arr1[2]?Does it changes arr2[2]?

As per the answer to the above question, no it will not. However, if you were to change your initialization mechanism as per the one I pointed out, it would.

When we pass an array (random array, not necessarily arr1 or arr2) from main into a method the compiler makes a shallow copy of the array to the method, right?

Your method will receive a location where to find the array, no copying will be done.

If the Java compiler was re-written to make and send a deep copy of the array data to the method. What happens to the original array in the main? I think the original array may change according to the method and pass back into the main. I am not sure.

If you were to create a deep copy of the array and send it to your method, and your method will work on that copy, then, the original array should stay the same.

OTHER TIPS

Initalization of an array with values will always allocate new memory. For "int[]arr1 ={1,2,3,4,5};" The jvm will count the length of the initialization array and allocate the required amount of space in the memory. In this case jvm allocated memory for 5 integers. when you do "int []arr2={1,2,3,4,5}", the same thing happens again ( jvm allocates memory for another 5 integers). Thus changing arr1[2] will not reflect arr[2].

arr1[2]=10;
 System.out.println(arr2[2]); // this will still print 3

If you wanted arr2 to point to the contents of arr1, you should do this:

int []arr2=arr1;

This would be a shallow copy. This makes an array reference object containing the same value as arr1. Now if you do :

arr1[2]=10;
System.out.println(arr2[2]); //this will print 10.

Now, if you want to do a deep copy of an array( Instead of duplicate initialization as you did), the right command would be:

int arr2[] = Arrays.copyOf(arr1, arr1.length);

This will behave like the first scenario ( your code - Changing arr1 will not affect arr2).

Because you have primitive types, you create independent arrays. To demonstrate deep and shallow copy:

MyObject a = new MyObject("a");
MyObject[] first = new MyObject[] {a};
MyObject[] second = new MyObject[] {a};
a.setName("b");
System.out.println(second[0].getName());
second[0].setName("c");
System.out.println(first[0].getName());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top