Shallow and deep cloning in Java - posted by Amit Satpute
Explain Shallow and deep cloning.
Answer
Cloning of
objects can be very useful if you use the prototype pattern or if you want to
store an internal copy of an object inside an aggregation class for example.
Deep cloning - You clone the object and their constituent parts.
It should be used when it is inappropriate to separate the parts, the object is
formed of, from it.
Shallow cloning - You clone only the object, not their parts. You add references
to their parts.
It should be used when it is adequate to have the references added to the cloned
object
Shallow and deep cloning in Java - posted by Vidya Sagar
What is Shallow and deep cloning in Java?
Cloning refers to creating duplicate copies of objects in java.
Shallow Cloning: Shallow cloning is a bitwise copy of an
object. New object is created which is an exact copy that of the original one.
In case any objects are referring the fields of these objects, just the
references are copied.
Deep Cloning: In deep cloning, complete duplicate copy of the
original copy is created. Deep cloning creates not only the primitive values of
the original objects but also copies all its sub objects as well.
Clonable interface is used to perform cloning in java.
Related Java links
Answer: Comparators can be used to control the order of certain
data structures and collection of objets too......
Answer: Private Constructors can't be access from any derived
classes neither from another class......
Answer: A static initializer block resembles a method with no
name, no arguments, and no return type......
Answer: To add any primitive to a collection, you need to
explicitly box (or cast) it into an appropriate wrapper class.....
|