Java Array Clone – Deep Copy vs Shallow Copy

Learn to create clone of an array in Java with example. Clone creates shallow copy of an array. Also learn to create array deep copy.

1. Array Clone – Shallow Copy

In Java, to create clone of array, you should use clone() method of array. It creates a shallow copy of array.

Cloning always creates shallow copy of array. Any change (in original array) will be reflected in cloned array as well.

package com.howtodoinjava.demo;

public class ArrayCloneExample 
{
	public static void main(String[] args) 
	{
		Employee[] empArr = new Employee[2];	//Original array

		empArr[0] = new Employee(100, "Lokesh", "Gupta", new Department(1, "HR"));
		empArr[1] = new Employee(200, "Pankaj", "Kumar", new Department(2, "Finance"));
		
		Employee[] clonedArray = empArray.clone();	//Shallow copied array
		
		empArray[0].setFirstName("Unknown");
		empArray[0].getDepartment().setName("Unknown");
		
		//Verify the change in original array - "CHANGED"
		System.out.println(empArray[0].getFirstName());						//Unknown
		System.out.println(empArray[0].getDepartment().getName());			//Unknown
		
		//Verify the change in cloned array - "CHANGED"
		System.out.println(clonedArray[0].getFirstName());					//Unknown
		System.out.println(clonedArray[0].getDepartment().getName());		//Unknown
	}
}

2. Array Deep Copy

If you want to create deep copy of an array in Java, then use Apache Common’s SerializationUtils.clone( array ) method.

package com.howtodoinjava.demo;

import org.apache.commons.lang3.SerializationUtils;

public class ArrayCloneExample 
{
	public static void main(String[] args) 
	{
		Employee[] empArray = new Employee[2];
		
		empArray[0] = new Employee(100, "Lokesh", "Gupta", new Department(1, "HR"));
		empArray[1] = new Employee(200, "Pankaj", "Kumar", new Department(2, "Finance"));
		
		Employee[] copiedArray = SerializationUtils.clone(empArray);	//Deep copied array
		
		empArray[0].setFirstName("Unknown");
		empArray[0].getDepartment().setName("Unknown");
		
		//Verify the change in original array - "CHANGED"
		System.out.println(empArray[0].getFirstName());						//Unknown
		System.out.println(empArray[0].getDepartment().getName());			//Unknown
		
		//Verify the change in deep copied array - "UNCHANGED"
		System.out.println(copiedArray[0].getFirstName());					//Lokesh
		System.out.println(copiedArray[0].getDepartment().getName());		//HR
	}
}

3. Complete Code

Complete code for creating shallow and deep copy of array in java.

package com.howtodoinjava.demo;

import org.apache.commons.lang3.SerializationUtils;

public class ArrayCloneExample 
{
	public static void main(String[] args) 
	{
		Employee[] empArray = new Employee[2];
		
		empArray[0] = new Employee(100, "Lokesh", "Gupta", new Department(1, "HR"));
		empArray[1] = new Employee(200, "Pankaj", "Kumar", new Department(2, "Finance"));
		
		Employee[] clonedArray = empArray.clone();						//Array clone
		
		Employee[] copiedArray = SerializationUtils.clone(empArray);	//Deep copied array
		
		empArray[0].setFirstName("Unknown");
		empArray[0].getDepartment().setName("Unknown");
		
		//Verify the change in original array - "CHANGED"
		System.out.println(empArray[0].getFirstName());						//Unknown
		System.out.println(empArray[0].getDepartment().getName());			//Unknown
		
		//Verify the change in cloned array - "CHANGED"
		System.out.println(clonedArray[0].getFirstName());					//Unknown
		System.out.println(clonedArray[0].getDepartment().getName());		//Unknown
		
		//Verify the change in deep copied array - "UNCHANGED"
		System.out.println(copiedArray[0].getFirstName());					//Lokesh
		System.out.println(copiedArray[0].getDepartment().getName());		//HR
	}
}
package com.howtodoinjava.demo;

import java.io.Serializable;

public class Employee implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String firstName;
	private String lastName;
	private Department department;

	public Employee(int id, String fName, String lName, Department department) {
		super();
		this.id = id;
		this.firstName = fName;
		this.lastName = lName;
		this.department = department;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}
}
package com.howtodoinjava.demo;

import java.io.Serializable;

public class Department implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	Integer id;
	String name;
	
	public Department(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + "]";
	}
}

Happy Learning !!

References:

SerializationUtils Java Doc

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial