CodesJava

Easy learning with example program codes

Difference between shallow copy and deep copy in java


Before discussing differences between shallow copy and deep copy let’s discuss what a copy is? We can have two types of copies:

  1. Reference copy: It creates a copy of a reference variable pointing to an object.
  2. Object copy: It creates a copy of the object itself.

Shallow copy

Shallow means having little depth.

Shallow copy of an object create a new object and copies all the field of object to the new object (either it is primitive or a reference). In case of non-primitive types only reference will be copied to the new instance. Therefore both original object and new object will point to the same non-primitive type fields i.e. objects. It returns an instance of Object class we have to explicitly cast to the original object.

Let’s consider the below example, we have a Customer class which have two properties name and address. In case of shallow copy a new Customer object will be created and as both name, address fields are of non-primitive type there references will be copied to new object. Now both the original object and copied object will point to the same name and address objects.

Here what is important that in case of name values will not reflect in other if we change it in one object. This is because of immutable nature of string objects.

public class Customer implements Cloneable {
    private String name;
    private Address address;
    public Customer(String name, Address address) {
         this.name = name;
         this.address = address;
    }
 
    //Getter Setter
 
    Public Object clone throws CloneNotSupportedException {
	return super.clone();
    }    
}

Deep copy

Deep copy of an object create a fully independent copy of an object. In case of deep copy everything will be copied to the new object. After deep copy, original object and copied object will refers to the different primitive and non-primitive fields. Changes on one object’s fields will not reflects in other’s.

Let’s consider the same example which we discussed earlier. We have a Customer class which have two properties name and address. In case of deep copy a new independent Customer object will be created. Now the original object and copied object will point to the different name and address objects.

Here what is important that in case of name values will not reflect in other if we change it in one object. This is because of immutable nature of string objects. So we do not have to do explicit cloning for that.

public class Customer implements Cloneable {
    private String name;
    private Address address;
    public Customer(String name, Address address) {
         this.name = name;
         this.address = address;
    }
 
    //Getter Setter
 
    //Override clone method
    @Override
    Public Object clone throws CloneNotSupportedException {
	Customer clonedObj = (Customer)super.clone();
	clonadObj.address = this.address.clone();
	return clonedObj;
       }	
}

Note:

  • clone() method of the object class support shallow copy of the object.
  • In case of deep copy we have to override the clone() method of the object class and we also need to ensure that all the member class also implement the Cloneable interface.
  • There is no difference between shallow and deep copy in java if only primitive type fields or Immutable objects are there as fields in the original object.
Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap