Shallow copy and Deep Copy in Cloud Computing - Salesforce

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 TrainingSalesforce Lightning Training
Salesforce Advanced Developer TrainingSalesforce Developer Training
Salesforce IoT TrainingSalesforce App Builder Certification Training
Salesforce AppExchange TrainingSalesforce Service Cloud Training
and many more.. 

 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Salesforce TrainingNov 19 to Dec 04View Details
Salesforce TrainingNov 23 to Dec 08View Details
Salesforce TrainingNov 26 to Dec 11View Details
Salesforce TrainingNov 30 to Dec 15View Details
Last updated: 04 Apr 2023
About Author

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.

read less
  1. Share:
Salesforce Articles