Home  >  Blog  >   Salesforce  > 

Shallow copy and Deep Copy in Cloud Computing - Salesforce

Rating: 4
  
 
4036
  1. Share:
Salesforce Articles

Shallow Copy and Deep Copy 

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.
Deep Copy
In this figure, the MainObject1 have fields “field1” of int type, and “ContainObject1” of ContainObject type. When you do a deep copy of MainObject1, MainObject2 is created with “field3” containing the copied value of “field1” and “ContainObject2” containing the copied value of ContainObject1.So any changes made to ContainObject1 in MainObject1 will not reflect in MainObject2.

MindMajix Youtube Channel

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 ();
two collections

  • Acc1 is handled for object acc1 à reference
  • Acc2 is handled for the object acc 2 à reference
  • Acc1 = acc2

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

  • Acc1 = acc2. Colne ();    → deep copy

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();
}
}
handles
No. of objects created is 1
Actually, variable are three types
1.Local variables → belongs to the method
2.Object variables → belongs to object
3.Class variables → belongs to a class
4.A class variable belongs to the class only not to the other object
→ CV existence is independence of objects

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

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 ();
}
}
class destribution
No. of objects 3
We can call the same methods through
Class name. method name
Eg:  counter. Get count ();
Public static void get count() {
System.debug c ‘count of objects =’+ count);
}
We can call the static methods, without caption of the object
Object variables count accesses through static method,
But we can access static variables only
Eg: public static void get count (){
//static var à system debug (‘count of objects =’ + count);
// object var à system debug (‘num =+num);
}

                       Check Out Salesforce Tutorials

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training Salesforce Lightning Training
Salesforce Advanced Developer Training Salesforce Developer Training
Salesforce IoT Training Salesforce App Builder Certification Training
Salesforce AppExchange Training Salesforce Service Cloud Training
and many more..  

 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Salesforce TrainingAug 05 to Aug 20
Salesforce TrainingAug 08 to Aug 23
Salesforce TrainingAug 12 to Aug 27
Salesforce TrainingAug 15 to Aug 30
Last updated: 04 August 2023
About Author
Remy Sharp
Arogyalokesh

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.

Recommended Courses

1 /15