Deep Copy vs. Shallow Copy in Scala

Scala | Deep Copy vs. Shallow Copy: Here, we will learn about deep copy and shallow copy in Scala. Both are object coping techniques.
Submitted by Shivang Yadav, on December 20, 2020

Prerequisite: Scala objects

Object Copy is a way to copy the contents of an object to another object.

There can be two methods to copy objects in Scala,

  1. Deep Copy
  2. Shallow Copy

1) Deep Copy

This a method of copying contents from one object to another object. In this copying technique, the changes made in the copied object are not reflected in the original object.

This shows that the new object copy created and the original object do not share the same memory location.

The figure below illustrates the Deep Copy,

deep copy

2) Shallow Copy

The shallow copy method of copying contents is a technique in which the changes made in the copied object are reflected in the original object.

This shows that the new object copy created and the original object share the same memory location.

The figure below illustrates the Shallow Copy,

shallow copy

Note: Shallow copy is used in object creation for case class in Scala.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.