Mindmajix

Shallow copy and Deep Copy in Cloud Computing

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.

Shallow Copy

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.

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

Screenshot_210

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

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

}

}

Screenshot_211

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

A class variable belongs to the class only not to the other object

→ CV existence is independence of objects

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

}

}

Screenshot_212

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);

}

Enroll for Instructor Led Live Online SalesForce Training


0 Responses on Shallow copy and Deep Copy in Cloud Computing"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.