Question

I have a little confusion about object cloning in java. What is the main difference between shallow cloning and deep cloning?

And which is much better ?

Was it helpful?

Solution 2

In shallow cloning, cloned class doesn't copy its internal objects but in deep cloning all internal objects of the class are also copied. One disadvantage of deep cloning that seem is with serialization, that serialization of such cloned object is not that easier.
However it can not be said which is better.

OTHER TIPS

What is the main difference between shallow cloning and deep cloning?

Shallow cloning copies the top level of a tree, but anything pointed to from that top level (e.g., object properties) remains shared by both copies. Deep cloning copies all levels of the tree, leaving no links between the source and the copy.

For instance, say you have a Person object (a) with a spouse property, which is also a Person object:

+-------------+
| Person: a   |
+-------------+
| name: "Joe" |               
| spouse      |-------------->+---------------+
+-------------+               | Person        |
                              +---------------+
                              | name: "Mary"  |
                              +---------------+

If you do a shallow clone of a to b, both a and b point to the same Person from their spouse properties:

+-------------+
| Person: a   |
+-------------+
| name: "Joe" |               
| spouse      |------+
+-------------+      |        
                     |
+-------------+      +------->+---------------+
| Person: b   |      +------->| Person        |
+-------------+      |        +---------------+
| name: "Joe" |      |        | name: "Mary"  |
| spouse      |------         +---------------+
+-------------+       

If you do a deep clone, you not only clone a to b, but you clone a.spouse to b.spouse so that they each end up having their own copy.

+-------------+
| Person: a   |
+-------------+
| name: "Joe" |               
| spouse      |-------------->+---------------+
+-------------+               | Person        |
                              +---------------+
+-------------+               | name: "Mary"  |
| Person: b   |               +---------------+
+-------------+       
| name: "Joe" |       
| spouse      |-------------->+---------------+
+-------------+               | Person        |
                              +---------------+
                              | name: "Mary"  |
                              +---------------+

And which is much better ?

Neither. Both have uses, and both can be misused.

Shallow Copy: Generally clone method of an object, creates a new instance of the same class and copies all the fields to the new instance and returns it. This is nothing but shallow copy.

Deep cloning

It is the desired behavior in most the cases. We want a clone which is independent of original and making changes in clone should not affect original.

you can think of copy constructor as well.

and aboput your question which one will be better, It depends on what behaviour you always looks for.

A brif introduction about cloning :http://howtodoinjava.com/2012/11/08/a-guide-to-object-cloning-in-java/

Consider b class has been cloned from a class.. If any changes done with a class affects b class,then it is shadow copy..if any changes done in a class does not affect b class,then it is called deep cloning.. Because in shadow cloning b class points to the a class..it shares the same memory location.. In deep cloning, seperate copy will be created.. It means a class and b class will have seperate memory location..

It depends on your requirement, either you need a different object which is internally pointing the same objects or you need a duplicate

Shallow Cloning is the default cloning strategy provided by Object.clone() method. Shallow cloning strategy creates a new instance and copies all fields of the given object to that new instance (either it is primitive or reference). So in the case of reference types only reference bits gets copied to the new instance, therefore, a reference variable of both objects will point to the same object.

Deep Cloning As the name suggest deep cloning means cloning everything from one object to another object. To achieve this you will need to trick your clone() method provide your own cloning strategy. You can do it by implementing Cloneable interface and override clone() method in every reference type we have in our object hierarchy and then call super.clone() and these clone() methods in our object’s clone method.

public Person clone() throws CloneNotSupportedException {
    Person clonedObj = (Person) super.clone();
    clonedObj.city = this.city.clone();
    return clonedObj;
}

To know more youu can check Java Cloning and Types of Cloning (Shallow and Deep) in Details with Example

It depends on what you want to achieve:

If you want a list of references to the same objects and just want to somehow avoid the ConcurrentModificationException than you could use shallow cloning.

If you want a whole bunch of new objects with just the same values inside you need deep cloning.

well, both shallow cloning and deep cloning are useful. If you have a single class instance which doesn't hold the reference of any other class then it's better to make a shallow copy but if there are some other class whose instance is used by our concrete class then we should choose deep cloning otherwise complete cloning cant be achieved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top