I am new to java can some one tell me please. Is it
Shallow copy
: primitive types and references are copied
Deep copy
: objects are copied recursively
There is no default implementation for clone()
I am new to java can some one tell me please. Is it
Shallow copy
: primitive types and references are copied
Deep copy
: objects are copied recursively
There is no default implementation for clone()
You can look at the documentation for clone()
:
The method
clone
for classObject
performs a specific cloning operation. First, if the class of this object does not implement the interfaceCloneable
, then aCloneNotSupportedException
is thrown. Note that all arrays are considered to implement the interfaceCloneable
and that the return type of the clone method of an array typeT[]
isT[]
whereT
is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
http://en.wikipedia.org/wiki/Object_copy -It's a shallow copy by default, but it is definitely possible to override it to perform a deep copy instead.
If you want a quick and easy deep copy, use Apache's Lang package and their SerializationUtils.clone()