I want to copy it, so I can not do this:
int [] underow = orig;
This is not a very shallow copy. A copy is a discrete entity that is similar to the original, but not the original. In your example, you have two links pointing to the same object. When you create a copy, you must have two resulting objects: the original and the copy.
Here, everything you do to change the shallow will happen with orig , since they both point to the same object.
Shading comes into play when the object you are comparing has links to other objects inside it. For example, if you have an array of integers and you create a copy, now you have two arrays that contain the same integer values:
Original Array [0] [1] [2] [3] After copying: [0] <--- Original [0] [1] [1] [3] [2] [4] Copy ---> [3]
However, if you had an array consisting of objects (say objArr1 and objArr2 )? When you make a shallow copy, now you have two new array objects, but each corresponding record between the two arrays points to the same object (since the objects themselves were not copied, but only links).
Original Array: [0:]----> [object 0] [1:]----> [object 1] [2:]----> [object 2] [3:]----> [object 3]
After copying (note how the corresponding locations point to the same instances):
Original -> [0:]----> [object 0] <----[:0] <- Copy [1:]----> [object 1] <----[:1] [2:]----> [object 2] <----[:2] [3:]----> [object 3] <----[:3]
Now, if you change objArr1 , replacing the record or deleting the record, then < objArr2 does not work. However , if you change the object in objArr1[0] , which is reflected in objArr2[0] , as these locations point to the same object. Thus, in this case, although the container objects themselves are different, they contain references to the same object.
When you make a deep copy, you will have two new arrays, where each corresponding location points to different instances. Thus, you almost completely copy objects.
My professor said that for primitives, a shallow and deep copy is essentially the same, since we must copy over each index of the array.
The important difference is that when you copy an array of primitives, you exactly copy the values. Every time you get a new primitive. However, when you have an array of objects, you do have an array of object references. Therefore, when you create a copy, all you have done is create a new array that has copies of the links in the original array. However, these new copies of the links still point to the same corresponding objects. This is what is known as a shallow copy. If you have deeply copied the array, the objects referenced by each individual location will also be copied. So you will see something like this:
Original -> [0:]----> [object 0] Copy -> [0:]----> [copy of object 0] [1:]----> [object 1] [1:]----> [copy of object 1] [2:]----> [object 2] [2:]----> [copy of object 2] [3:]----> [object 3] [3:]----> [copy of object 3]
But setting the whole array is equal to another array, does the same thing, right?
No no. Here you simply create a new reference to an existing array:
arr1 -> [0, 1, 2, 3, 4]
Now let's say that you did arr2 = arr1 . You have:
arr1 -> [0, 1, 2, 3, 4] <- arr2
So here arr1 and arr2 point to the same array. Therefore, any modification performed using arr1 will be displayed when accessing the array using arr2 , since you are looking at the same array. This does not happen when you make copies.