Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Java Object Cloning Techniques and Copy Depth Analysis

Tech 1

Cloning is essential when an existing object's state needs to be duplicated while preserving the original instance for further processing. This ensures that modifications to the copy do not inadvertently alter the source data.

Implementation Approaches

  1. Cloneable Interface: By implemanting the Cloneable marker interface and overriding the clone() method, developers can invoke Object.clone(). This provides a field-by-field copy.
  2. Serialization: Converting an object into a byte stream and reconstructing it creates a completely independent copy, effectively achieving deep cloning without manual field management.
  3. Utility Libraries: Frameworks like Apache Commons or Spring offer bean copying tools. These are convenient but generally perform shallow copies unless configured otherwise.

Shallow vs. Deep Copying

  • Shallow Copy: Duplicates primitive fields but copies references for objects. Both the original and the clone point to the same nested objects.
  • Deep Copy: Duplicates primitive fields and recursive clones all referenced objects. The clone possesses a fully independent object graph.

Code Demonstration: Shallow Copy

The following example illustrates shallow copying. Note that modifying the nested Location object in the original User instance affects the cloned instance because they share the same reference.

public class ShallowCopyDemo {
    public static void main(String[] args) {
        User original = new User();
        original.setUserId(101);
        original.setUserName("Alice");
        Location loc = new Location();
        loc.setCity("New York");
        original.setUserLocation(loc);

        try {
            User copy = (User) original.clone();
            System.out.println("--- Shallow Copy Verification ---");
            System.out.println("Same Instance? " + (original == copy));
            System.out.println("Same Location Reference? " + (original.getUserLocation() == copy.getUserLocation()));

            // Mutating nested object
            original.getUserLocation().setCity("Boston");

            System.out.println("Original Location: " + original.getUserLocation().getCity());
            System.out.println("Copy Location: " + copy.getUserLocation().getCity());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

class User implements Cloneable {
    private int userId;
    private String userName;
    private Location userLocation;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    // Getters and Setters omitted for brevity
}

class Location {
    private String city;
    // Getters and Setters omitted for brevity
}

Code Demonstration: Deep Copy

To achieve a deep copy, the cloning logic must explicitly duplicate nested mutable objects. Below, the clone() method is overridden to create a new instance of the Location class.

public class DeepCopyDemo {
    public static void main(String[] args) {
        User original = new User();
        original.setUserId(101);
        original.setUserName("Alice");
        Location loc = new Location();
        loc.setCity("New York");
        original.setUserLocation(loc);

        try {
            User copy = (User) original.deepClone();
            System.out.println("--- Deep Copy Verification ---");
            System.out.println("Same Instance? " + (original == copy));
            System.out.println("Same Location Reference? " + (original.getUserLocation() == copy.getUserLocation()));

            // Mutating nested object
            original.getUserLocation().setCity("Boston");

            System.out.println("Original Location: " + original.getUserLocation().getCity());
            System.out.println("Copy Location: " + copy.getUserLocation().getCity());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class User implements Cloneable {
    private int userId;
    private String userName;
    private Location userLocation;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public User deepClone() throws Exception {
        User cloned = (User) super.clone();
        if (this.userLocation != null) {
            cloned.userLocation = (Location) this.userLocation.clone();
        }
        return cloned;
    }
}

class Location implements Cloneable {
    private String city;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    // Getters and Setters omitted for brevity
}
Tags: Javacloning

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.