top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What Is Shallow Copy and Deep Copy?

+3 votes
302 views
What Is Shallow Copy and Deep Copy?
posted Mar 13, 2014 by Pavan P Naik

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

The action of copying the attributes of one object into another of same data type is called object copy. In java, we have the following approaches of copying one object into another:

Shallow Copy: here if the field which is to be copied is a primitive type, then the value is copied else if the field which is to be copied is a memory address (or an object itself) then the address is copied. Thus if the address is changed by one object, the change gets reflected everywhere.

Deep Copy: here the data is copied in both the situations. This approach is costlier and slower.

Lazy Copy: This is a combination of the above two approaches. Initially the shallow copy approach is used and then checked if the data is shared by many objects and the program needs to modify an object, the deep copy approach is used.

answer Mar 13, 2014 by Amit Kumar Pandey
...