java.util.Calendar.clone() returns "...a new Calendar with the same properties" and returns "a shallow copy of this Calendar".
This does not appear to be a shallow copy as answered here on SO. That question is tagged language-agnostic, Java does not seem to follow the language agnostic definition. As I step through the code I notice that the structure and the elements are copied to this new object, more than the language agnostic structure only.
In Java, what is a shallow copy?
How does it differ from a Java deep copy (if that exists)?
A shallow copy just copies the object reference into the target reference. It does not create a new object on the heap. By default, Java does shallow cloning using clone() function.
To get a new object on the heap, one has to perform deep cloning which can be implemented by Serialization and De-serialization.
It appears to be a mistake in the documentation. I don't see how anything that Android's Calendar.clone method does meets the typical definition (in Java or otherwise) of a "shallow copy".
A shallow copy is a copy of the reference pointer to the object, whereas a deep copy is a copy of the object itself. In Java, objects are kept in the background, what you normally interact with when dealing with the objects is the pointers. The variable names point to the memory space of the object. A shallow copy is made when you set one variable equal to another like so:
A deep copy could be made by getting the properties of object A and putting them in a new object B.
This affects program behavior in that if you make a shallow copy and perform a task on it, that affects all shallow copies of the object. If you make a change to a deep copy, only that copy is affected. I hope this is detailed enough for you.
Shallow copy is a just a set of pointers to the same memory locations. Actually it does not create a real copy so the memory usage is lower.
In a case of a deep copy, an exact copy of the memory segment is created and pointers are set to new memory locations. So theoritically the memory consumption should be twice in this case.
A shallow copy just copies the values of the references in the class. A deep copy copies the values. given:
In this case the shallow copy has the same reference (
==
) and the deep copy only has an equivalent reference (.equals()
).If a change is made to the value of a shallowly copied reference, then the copy reflects that change because it shares the same reference. If a change is made to the value of a deeply copied reference, then the copy does not reflect that change because it does not share the same reference.
C-ism
Result:
In a shallow copy,the clone object has a copy of primitive values but the object references refer to the same objects as the original copy. Shallow Copies have a significant drawback, cloned object and original copy refer to the same address object. Any change that cloned object makes in address object will also be reflected in original copy, which is an unwanted behaviour. What we really wanted is two separate copies of user object. Deep copying comes to our rescue for this kind of situation.
Deep copying clones not just the primitive values, it also creates copies of object references.
You can have a look at working example on this at here :https://codingninjaonline.com/2017/11/09/deep-vs-shallow-copy/