+ Start a Discussion
Nitesh Kumawat 5Nitesh Kumawat 5 

what is exact difference between clone and deep clone

Hi Nitesh,

High level I will say deepclone and clone are to copy the record .You have idea on trigger.old and trigger.new in Trigger class . clone is same as trigger.new and deepclone is same as trigger.old .
In memory level clone copy the reference and deepclone does not copy the reference .

You can test and feel some high level with below code in your dev org.
public class CloneDeepCloneTest{
    public List<Account> newAccList{get;set;}
    public List<Account> newAccListCloned;
    public List<Account> oldAccList;
  public CloneDeepCloneTest(){
    newAccList=[SELECT id,Name,Industry FROM Account LIMIT 3];
   public PageReference doSave() {
      update newAccList;
    System.debug('*******deepClone value********'+oldAccList);
        return null;
<apex:page Controller="CloneDeepCloneTest">
  <apex:form >
  <apex:pageBlock >
      <apex:repeat value="{!newAccList}" var="nA" >
         <apex:inputField value="{!nA.Name}"/>
      <apex:commandButton value="Save" action="{!doSave}"/>

Set debug  log , observe and enjoy !!!
Clone is analogous to Pass By Reference while DeepClone is analogous to Pass By Value.

When you are cloning, just a pointer is being created to the already existing record/collection. Any changes that you do on Clone are applied on Original too and vice-versa (actually the record/collection is just 1 while the pointers are 2).
In DeepClone, an exact replica of the record/collection is being created somewhere else in memory, changes to the deepClone or the original are independent of each other and would not reflect on the other.
If this is resolved, please do mark the question as Resolved and the most appropriate/helpful answer as the best answer :).
If none of the answers helped you significantly, please post the solution. You may also mark your solution as the best answer.
Phil WPhil W
Apex supports clone against Object, not just SObject. I found the documentation for "clone" to be inadequate and I had to write some test code to see what the actual behaviour is, at least in the context of Map and List.

The "clone" method documentation should describe how just the "root object" is cloned (the object on which clone is called), whilst nested/referenced objects are maintained by reference (and not copied by value), whether these are SObjects or other Apex objects.

For example, cloning a Map<String, List<String>> will create a new map with new map entries but the same keys and values; each value (a list) is shared between the maps. Since lists are mutable this is problematic; updating a list from one map will update it in the other map (since it is shared).

Unfortunately, deepClone cannot be used with anything that isn't SObject based. The cloner code must do more. Continuing this example, the values can be cloned too via:

for (String key : myMap.keySet()) {
    myMap.put(key, myMap.get(key).clone());

Since String instances are immutable there is no harm sharing these between maps/lists/sets/objects.

Such an example really should appear in the documentation.