Loyola College in Maryland

CS 301 - Data Structures and Algorithms I
Spring 2003


Loyola College > Department of Computer Science > CS 301 > Examples > Shallow vs. Deep Copy

Code

// This code uses Javadoc/doxygen style comments.  Click
// here to see the documentation that
// is automatically produced from these comments.

/**
 * A simple application to demonstrate the difference between deep copy
 * and shallow copy.
 *
 * @author Jim Glenn
 * @version 0.1 2/14/2003
 */

public class Copy
{
    /**
     * A simple object with a dynamically allocated array inside it.
     */

    public static class ArrayObject
    {
	private int[] arr;

	/**
	 * Creates an object with an array of the given size.
	 *
	 * @param size the size of the array inside this object
	 */

	public ArrayObject(int size)
	{
	    arr = new int[size];
	}

	/**
	 * Creates an object holding the given array.
	 *
	 * @param a the array to put in the new object
	 */

	private ArrayObject(int[] a)
	{
	    arr = a;
	}

	/**
	 * Returns a shallow copy of this object.  This object and
	 * the copy will share the same array.
	 *
	 * @return a shallow copy of this object
	 */

	public ArrayObject shallowCopy()
	{
	    return new ArrayObject(arr);
	}

	/**
	 * Returns a deep copy of this object.  The copy with have its
	 * own array whose elements are initialized to be the same as
	 * this one's.
	 *
	 * @return a deep copy of this object
	 */

	public ArrayObject deepCopy()
	{
	    ArrayObject copy = new ArrayObject(new int[arr.length]);
	    for (int i = 0; i < arr.length; i++)
		copy.arr[i] = arr[i];

	    return copy;
	}

	/**
	 * Sets the value at the given location in this object's array.
	 *
	 * @param index the location in the array to set
	 * @param value the value to store at that location
	 * @throws ArrayIndexOutOfBoundsException when index is not valid
	 */

	public void set(int index, int value)
	{
	    arr[index] = value;
	}

	/**
	 * Returns the value at the given location in this object's array.
	 *
	 * @param index the location in the array to examine
	 * @return the value at that location
	 * @throws ArrayIndexOutOfBoundsException when index is not valid
	 */
	
	public int get(int index)
	{
	    return arr[index];
	}

	/**
	 * Returns a string representation of this object.
	 * The elements will be displayed (elt 0 first) between
	 * square brackets.
	 *
	 * @return a string representation of this object
	 */

	public String toString()
	{
	    StringBuffer result = new StringBuffer();
	    result.append('[');
	    for (int i = 0; i < arr.length; i++)
		{
		    if (i > 0)
			result.append(' ');
		    result.append(arr[i]);
		}
	    result.append(']');

	    return result.toString();
	}
    }

    /**
     * Driver to demonstrate shallow vs. deep copy.
     */

    public static void main(String[] args)
    {
	// make an object

	ArrayObject original = new ArrayObject(10);
	for (int i = 0; i < 10; i++)
	    original.set(i, i);
	
	// make a shalow copy of the original object

	ArrayObject shallowCopy = original.shallowCopy();
	
	// look, they're the same!

	System.out.println(original);
	System.out.println(shallowCopy);

	// modify the shallow copy

	shallowCopy.set(5, 55);

	// look, they're still the same!

	System.out.println("----------------------");
	System.out.println(original);
	System.out.println(shallowCopy);

	// make a deep copy of the original object

	ArrayObject deepCopy = original.deepCopy();

	// look, they're still the same!

	System.out.println("----------------------");
	System.out.println(original);
	System.out.println(deepCopy);

	// modify the deep copy

	deepCopy.set(6, 66);

	// now they're different!

	System.out.println("----------------------");
	System.out.println(original);
	System.out.println(deepCopy);
    }
}

Output

[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
----------------------
[0 1 2 3 4 55 6 7 8 9]
[0 1 2 3 4 55 6 7 8 9]
----------------------
[0 1 2 3 4 55 6 7 8 9]
[0 1 2 3 4 55 6 7 8 9]
----------------------
[0 1 2 3 4 55 6 7 8 9]
[0 1 2 3 4 55 66 7 8 9]