int [] arr1 = new int[]{4,6,3,2,7,8};
int [] arr2 = new int[]{6,7,5,3,2,9};
arr2 = arr1;
System.out.println(arr2[3]); // 2 ( =arr1[3] )
The output of this code is 2.
So what's exactly happening here?
Is it that arr1 and arr2 are in different memory locations but arr2 is pointing at the values of arr1?
What happened to the original values of arr2?
Are they still accessible or were they removed by garbage collection?
if they are accessible, does the programmer have to free the memory of the 2nd array?
arr1 and arr2 are both references.
Initially they refer to different arrays.
When you write arr2 = arr1;, nothing is referring to the second array in your program and so it is indeed eligible for garbage collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With