# Shallow Copy vs Deep Copy in Java

<div><strong>Source</strong>: <a href='https://www.tuanh.net/blog/java/-shallow-copy-vs-deep-copy-in-java'> Shallow Copy vs Deep Copy in Java</a></div><div class="line-post"> 
 <h2 class="title-post">1. Shallow Copy in Java</h2> 
</div>
<div class="line-post">
  A shallow copy of an object is a new object where the fields are copied directly from the original. But beware: for fields that are references, only the reference itself is copied, not the object it refers to. This section covers how to create a shallow copy and the implications of its use. 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.1 What is Shallow Copy?</h3> 
</div>
<div class="line-post">
  A shallow copy duplicates the top-level structure of the original object, leaving any nested objects as references. It works well for simpler structures but can cause issues when objects are deeply nested. 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.2 How to Create a Shallow Copy</h3> 
</div>
<div class="line-post">
  In Java, the clone() method (if implemented) is the primary way to perform a shallow copy. Here’s how it works: 
</div>
<pre class="code"><span class="keyword-css">class</span> ShallowCopyExample <span class="keyword-css">implements</span> Cloneable {<br>    <span class="keyword-css">int</span> value;<br>    <span class="keyword-css">int</span>[] array;<br><br>    <span class="keyword-css">public</span> ShallowCopyExample(<span class="keyword-css">int</span> value, <span class="keyword-css">int</span>[] array) {<br>        <span class="keyword-css">this</span>.value = value;<br>        <span class="keyword-css">this</span>.array = array;<br>    }<br><br>    @Override<br>    <span class="keyword-css">protected</span> Object clone() <span class="keyword-css">throws</span> CloneNotSupportedException {<br>        <span class="keyword-css">return</span> <span class="keyword-css">super</span>.clone();<br>    }<br>}<br><br><span class="keyword-css">public</span> <span class="keyword-css">class</span> Main {<br>    <span class="keyword-css">public</span> <span class="keyword-css">static</span> <span class="keyword-css">void</span> main(String[] args) <span class="keyword-css">throws</span> CloneNotSupportedException {<br>        <span class="keyword-css">int</span>[] array = {1, 2, 3};<br>        ShallowCopyExample original = <span class="keyword-css">new</span> ShallowCopyExample(10, array);<br>        ShallowCopyExample copy = (ShallowCopyExample) original.clone();<br><br>        System.out.println("Original array before change: " + original.array[0]); // 1<br>        copy.array[0] = 99;<br>        System.out.println("Original array after change: " + original.array[0]); // 99<br>    }<br>}</pre>
<div class="line-post"> 
 <h3 class="sub1">1.3 Pros and Cons of Shallow Copy</h3> 
</div>
<div class="line-post"> 
 <ul> 
  <li><strong>Pros</strong>: Fast and memory-efficient, useful for simple objects.</li> 
  <li><strong>Cons</strong>: Not ideal for objects with nested or mutable references.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.4 Common Mistakes with Shallow Copy</h3> 
</div>
<div class="line-post">
  Failing to understand shallow copies can lead to bugs. For instance, when one element of a copied object is changed, and it inadvertently affects the original object. This section demonstrates how to avoid such pitfalls. 
</div>
<div class="line-post"> 
 <h2 class="title-post">2. Deep Copy in Java</h2> 
</div>
<div class="line-post">
  A deep copy duplicates an object and all objects it references, recursively. This method prevents unintentional changes, making it ideal for complex object structures. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.1 What is Deep Copy?</h3> 
</div>
<div class="line-post">
  A deep copy creates a distinct object where nested objects are also duplicated, ensuring no reference sharing. This guarantees complete independence between the original and copied objects. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.2 How to Create a Deep Copy</h3> 
</div>
<div class="line-post">
  Java does not natively support deep copying as easily as shallow copying, but it can be achieved through custom cloning methods or serialization. Here’s an example using a custom copy constructor: 
</div>
<pre class="code"><span class="keyword-css">class</span> DeepCopyExample {<br>    <span class="keyword-css">int</span> value;<br>    <span class="keyword-css">int</span>[] array;<br><br>    <span class="keyword-css">public</span> DeepCopyExample(<span class="keyword-css">int</span> value, <span class="keyword-css">int</span>[] array) {<br>        <span class="keyword-css">this</span>.value = value;<br>        <span class="keyword-css">this</span>.array = array.clone();<br>    }<br>}<br><br><span class="keyword-css">public</span> <span class="keyword-css">class</span> Main {<br>    <span class="keyword-css">public</span> <span class="keyword-css">static</span> <span class="keyword-css">void</span> main(String[] args) {<br>        <span class="keyword-css">int</span>[] array = {1, 2, 3};<br>        DeepCopyExample original = <span class="keyword-css">new</span> DeepCopyExample(10, array);<br>        DeepCopyExample copy = <span class="keyword-css">new</span> DeepCopyExample(original.value, original.array);<br><br>        System.out.println("Original array before change: " + original.array[0]); // 1<br>        copy.array[0] = 99;<br>        System.out.println("Original array after change: " + original.array[0]); // 1<br>    }<br>}</pre>
<div class="line-post"> 
 <h3 class="sub1">2.3 Pros and Cons of Deep Copy</h3> 
</div>
<div class="line-post"> 
 <ul> 
  <li><strong>Pros</strong>: Prevents unintended modifications to the original data.</li> 
  <li><strong>Cons</strong>: Can be slower and more memory-intensive than shallow copies, especially for large, deeply nested objects.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.4 Common Mistakes with Deep Copy</h3> 
</div>
<div class="line-post">
  Deep copying can lead to performance issues if not managed carefully. Explore various optimization techniques, such as avoiding redundant deep copies where not necessary. 
</div>
<div class="line-post"> 
 <h2 class="title-post">3. When to Use Shallow Copy vs. Deep Copy</h2> 
</div>
<div class="line-post">
  This section provides guidelines on when to choose shallow vs. deep copying. For example, use shallow copies for simple data transfers, while deep copies are preferable for data manipulation where integrity is essential. 
</div>
<div class="line-post"> 
 <h2 class="title-post">4. Conclusion</h2> 
</div>
<div class="line-post">
  Understanding shallow and deep copies helps prevent bugs in Java and ensures that object manipulation is both efficient and intentional. Are you ready to implement these techniques in your projects? If you have questions or need further clarification, feel free to leave a comment below! 
</div><p><strong>Read more at </strong>: <a href='https://www.tuanh.net/blog/java/-shallow-copy-vs-deep-copy-in-java'> Shallow Copy vs Deep Copy in Java</a></p>
