Understanding Shallow, Lazy and Deep Clone in Java
When you want to create copies of objects for various purposes in Java, there are three common approaches that can be used for object copying: shallow cloning, deep cloning, and lazy cloning. In this article, we will explore the concepts of shallow copy and deep copy in Java, their differences, and when to use each of them. Moreover, we will mention lazy copy and try to give examples of it.
If you don’t have a medium membership, you can use this link to reach the article without a paywall.
2. Explaining the Concepts
2.1. What is the Shallow Clone?
A shallow clone of an object in Java creates a new object but does not duplicate its nested objects. Instead, it copies references to the nested objects. In other words, a shallow clone creates a new instance of the outer object, but the inner objects are shared between the original and the cloned object. Changes made to the inner objects in the clone will affect the original, and vice versa.