Lets first separate it out and see what each one means.
What is Shallow Copy?
A Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied, i.e., only the memory address is copied.
In this figure, the MainObject1 have fields “field1” of int type, and “ContainObject1” of ContainObject type. When you make a shallow copy of MainObject1, MainObject2 is created with “field3” containing the copied value of “field1” and still pointing to ContainObject1 itself. Observe here and you will find that since field1 is of primitive type, the values of it are copied to field3 but ContainedObject1 is an object, so MainObject2 is still pointing to ContainObject1. So any changes made to ContainObject1 in MainObject1 will reflect in MainObject2.
What is Deep Copy?
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.
Let’s get into deep by differentiating them further :
Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.
Account acc1 = new account ();
Account acc2 = new account ();
Frequently Asked Salesforce Interview Questions & Answers
Shallow copying, means acc2 is handled for acc1 but the values of acc2 are not referred in acc1, only acc2 is acting as handle for the object acc1
Here the values of acc2 are transferred to acc1
Global class counter {
Integer NUM = 0;
Public counter () {
Num ++;
}
Public void get count (){
System debug (‘ the count is ‘ +num);
}
}
Global class test {
Public static test method void main (){
Counter c1= new counter ();
Counter c2 = new counter ();
c3. Get count();
}
}
Eg:
Global class counter {
Private integer num; 11 object variable
Private static integer counter = 0; 11 class variable
Public counter (){
Num = 0;
Count ++;
}
Public counter (integer num ){
This. Num = num
Count ++;
}
Public void static value (integer num ){
This. Num = num;
}
Public void get values (){
System. Debug (‘num = ‘ + num);
}
Public void get count (){
System. Debug (‘count of objects = ‘+count);
}
}
Global class test {
Public static test method void main (){
Counter c1 = new counter ();
Counter c2 = new counter ();
Counter c3 -= new counter (10);
c1. Get values ();
}
}
Check Out Salesforce Tutorials
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
Name | Dates | |
---|---|---|
Salesforce Training | Dec 05 to Dec 20 | View Details |
Salesforce Training | Dec 09 to Dec 24 | View Details |
Salesforce Training | Dec 12 to Dec 27 | View Details |
Salesforce Training | Dec 16 to Dec 31 | View Details |
Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.
1 / 15
Copyright © 2013 - 2023 MindMajix Technologies