Copy Constructor in Java

What is Copy Constructor? Sometimes a programmer wants to create an exact but separate copy of an existing object so that subsequent changes to the copy should not alter the original or vice versa. This is made possible using the copy constructor. A copy constructor is a constructor that creates a new object using an existing object of the same class. Copy constructor takes single argument which is of the type as the class itself in which the copy constructor is implemented. For example, let us assume a class namely…

Continue

Shallow Copy and Deep Copy in Java

Introduction Shallow Copy also means Shallow Clone and Deep Copy also means Deep Clone. Cloning an object is about creating the copy of the original object, i.e., making an identical copy of the original object. By default, cloning in Java is field by field copy i.e. as the Object class does not have idea about the structure of class on which clone() method will be invoked. Therefore JVM does the following things when called for cloning: If the class has only primitive data type members then a completely new copy…

Continue