Copy-by-value Vs Copy-by-reference

Copy by value

In a primitive data-type when a variable is assigned a value we can imagine that a box is created in the memory. This box has a sticker attached to it i.e. the variable name. Inside the box the value assigned to the variable is stored.

Figure 1: Variable assignment
Figure 2: Variables are copied
Figure 3: Variables are independent of each other

Copy by reference

In case of a non-primitive data-type the values are not directly copied. When a non-primitive data-type is assigned a value a box is created with a sticker of the name of the data-type. However, the values it is assigned is not stored directly in the box. The language itself assigns a different memory location to store the data. The address of this memory location is stored in the box created.

Figure 4: Copy by reference

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vivek Baruah

Vivek Baruah

Engineer, Programmer, Educationist, Aspirant