Cloning: Shallow vs. Deep Copies

Cloning: Shallow vs. Deep Copies

I am seeking to clone an object that contains anarray of the type float[][] which in turn contains primitive types. Using super.clone() and a clonable interface in the class has resulted in a shallow copy. Do I need to make the array an array of Objects and deep copy?

The default clone() method is implemented in native code in the fundamental java.lang.Object class. It performs the equivalent of a memcpy() operation, which in C would cause any pointers to be copied directly. The effect in Java is for all references to point to the same objects and for primitive types to be copied. This results in a shallow copy. A deep copy is where true copies of all references are made by creating a new object and performing a deep copy of the original object. If you want clone() to perform a deep copy, you have to implement it yourself. The following example makes clear the difference between a deep copy and a shallow copy, as well as showing that the default clone() method copies references, rather than creating new objects with equivalent values.

public final class CloneExample {    public static class ShallowCopy implements Cloneable {	// I'm using an int array so I don't have to do print formatting.	public int[][] data = {	    { 0, 1 },	    { 1, 0 }	};	public String toString() {	    StringBuffer buffer = new StringBuffer();	    int row, column, d[];	    for(row = 0; row < data.length; ++row) {		d = data[row];		for(column = 0; column < d.length; ++column) {		    buffer.append(d[column]);		    buffer.append(' ');		}		buffer.append('
');	    }	    return buffer.toString();	}	public void setValue(int row, int column, int value) {	    data[row][column] = value;	}	// Performs a default shallow copy	public Object clone() throws CloneNotSupportedException {	    return super.clone();	}    }    public static class DeepCopy extends ShallowCopy {	// Explicitly performs a deep copy	public Object clone() throws CloneNotSupportedException {	    int row;	    DeepCopy copy = new DeepCopy(); = new int[data.length][];	    for(row=0; row < data.length; ++row) {[row] = new int[data[row].length];		System.arraycopy(data[row], 0,[row], 0,				 data[row].length);	    }	    return copy;	}    }    public static final void main(String[] args) {	ShallowCopy shallow = new ShallowCopy();	ShallowCopy shallowClone;	DeepCopy deep = new DeepCopy();	DeepCopy deepClone;	System.out.println("ShallowCopy Original Data");	System.out.println(shallow.toString());	try {	    shallowClone = (ShallowCopy)shallow.clone();	} catch(CloneNotSupportedException e) {	    e.printStackTrace();	    return;	}	// The clone should have the same data	System.out.println("ShallowCopy Clone Data");	System.out.println(shallowClone.toString());	// In a shallow copy, if we alter the original data, it should	// be reflected in the cloned data.	shallow.setValue(0, 1, 0);	shallow.setValue(1, 0, 0);	System.out.println("ShallowCopy Original Data");	System.out.println(shallow.toString());	System.out.println("ShallowCopy Clone Data");	System.out.println(shallowClone.toString());	// Why was the data identical?  Because the shallow copy caused	// the clone data to reference the same array as the original.	if( ==	    System.out.println(	       "The original array and cloned array are the same object!");	else	    System.err.println("Unexpected result!");	System.out.println("DeepCopy Original Data");	System.out.println(deep.toString());	try {	    deepClone = (DeepCopy)deep.clone();	} catch(CloneNotSupportedException e) {	    e.printStackTrace();	    return;	}	// The clone should have the same data	System.out.println("DeepCopy Clone Data");	System.out.println(deepClone.toString());	// In a deep copy, if we alter the original data, it should	// NOT be reflected in the cloned data.	deep.setValue(0, 1, 0);	deep.setValue(1, 0, 0);	System.out.println("DeepCopy Original Data");	System.out.println(deep.toString());	System.out.println("DeepCopy Clone Data");	System.out.println(deepClone.toString());	// The data was not identical because the deep copy created	// an entirely new array.	if( !=	    System.out.println(	       "The original array and cloned array are NOT the same object!");	else	    System.err.println("Unexpected result!");    }}
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as