java - Array deep copy and shallow copy -
i'm learning deep copy , shallow copy.
if have 2 arrays:
int[]arr1={1,2,3,4,5}; int[]arr2={1,2,3,4,5};
question: both arrays point same references [1][2][3][4][5]
.
what happen if change arr1[2]
?does changes arr2[2]
?
when pass array (random array, not arr1
or arr2
) main method compiler makes shallow copy of array method, right?
if java compiler re-written make , send deep copy of array data method. happens original array in main? think original array may change according method , pass main. not sure.
question: both arrays point same references [1][2][3][4][5].
not exactly. arrays have identical content. being said, per initialization, data using not shared. initializing however:
int[]arr1={1,2,3,4,5}; int[] arr2 = arr1
what happen if change arr1[2]?does changes arr2[2]?
as per answer above question, no not. however, if change initialization mechanism per 1 pointed out, would.
when pass array (random array, not arr1 or arr2) main method compiler makes shallow copy of array method, right?
your method receive location find array, no copying done.
if java compiler re-written make , send deep copy of array data method. happens original array in main? think original array may change according method , pass main. not sure.
if create deep copy of array , send method, , method work on copy, then, original array should stay same.
Comments
Post a Comment