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)?
The 1.6 docs document
Calendar.clone
as "Creates and returns a copy of this object." A literal shallow copy as specified byObject.clone
wouldn't make any sense. Java uses the term "shallow copy" in a fairly typical sense.Where are you getting this documentation?
The official Java 6 docs on java.sun.com simply have Calendar.clone() returning a copy of the object. No mention of shallow.
More generally, a shallow copy in Java is one where you get a new object reference but the new object holds (directly or indirectly) references to data in the original.
For example:
returns a shallow copy in its clone().
First of all, the Javadoc of ArrayList is somewhat wrong if we are talking about one-dimensional arrays, as it uses the method copyOf in Arrays. So clone() gives back a one-dimensional copy, at least since 1.5 (I didn't test further)! So that's what "shallow" means in Java: one-dimensional
You can read more here: http://www.javapractices.com/topic/TopicAction.do?Id=3. So clone() is no shallow copy! If you want a real shallow copy of a one-dimensional array, just reference it:
Arrays in Java are tricky, also because Java does pass-by-value, but the values of arrays are only their pointers! In the other hand this allows us to synchronize the objects, which is a great thing. Still, there are some problems if you use arrays within arrays (or ArrayLists), because a clone() of the container array (or ArrayList) won't copy their values, only their references! So you simply shouldn't put any arrays into an array, you should only deal with objects in an array!
And Javadoc is difficult to understand sometimes, so give testing a try...
Have fun!