Java Shallow & Deep cloning


Java Shallow and Deep cloning

Shallow and deep cloning in Java - posted by Amit Satpute

Explain Shallow and deep cloning.

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.


Test your Java skills

Java part 1 (39 questions)
Java part 2 (40 questions)
EJB (20 questions)
JDBC (20 questions)
Applet (20 questions)
Struts (21 questions)
Servlets (20 questions)
Java web services (20 questions)

Write your comment - Share Knowledge and Experience

Related Java links

What is the purpose of Comparator Interface?

Answer: Comparators can be used to control the order of certain data structures and collection of objets too......

Explain the impact of private constructor.

Answer: Private Constructors can't be access from any derived classes neither from another class......

What are static Initializers?

Answer: A static initializer block resembles a method with no name, no arguments, and no return type......

Explain autoboxing and unboxing.

Answer: To add any primitive to a collection, you need to explicitly box (or cast) it into an appropriate wrapper class.....

Interview questions
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us