Shallow Copy Deep Copy Java

Shallow Copy Deep Copy Java

Shallow copy: Shallow copy is copy of reference of the object and it does not create real object of the class so no new memory will be allocated, no memory consumption. Below is java class example:

package com.javahonk;

public class ShallowCopy {

    public static void main(String[] args) {
        
        Person person = new Person();
        person.setName("Java Honk");

        try {
            Person javaHonk = (Person) person.clone();
            System.out.println(javaHonk.getName());

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

    }

}

class Person implements Cloneable {

    String name;
    Person myFriend;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person getMyFriend() {
        return myFriend;
    }

    public void setMyFriend(Person myFriend) {
        this.myFriend = myFriend;
    }

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

}

Output:

Shallow Copy Deep Copy Java

Deep copy: To make deep copy of the object you have to create object by yourself and if object contains some other objects then you should be careful copying the object and its value otherwise it can move into cyclic dependencies. Below are java example:

package com.javahonk;

public class DeepCopy {

    public static void main(String[] args) {
        
        Person person = new Person();
        person.setName("Java Honk");

        try {
            Person javaHonk = (Person) person.clone();
            System.out.println(javaHonk.getName());

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

    }

}

class Person implements Cloneable {

    String name;
    Person myFriend;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person getMyFriend() {
        return myFriend;
    }

    public void setMyFriend(Person myFriend) {
        this.myFriend = myFriend;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person person = new Person();
        person.setName(name);
        return person;
    }

}

Output:

Shallow Copy Deep Copy Java

One more example of deep copy:

package com.javahonk;

public class PersonTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        Persons person = new Persons();
        person.setName("Sergio");
        
        Persons persons = new Persons();
        
        person.setFriend(persons);
        person.getFriend().setName("Java Honk");
        
        Persons person2 =person.deepCopy();
        System.out.println(person2.getName());
        System.out.println(person2.getFriend().getName());

    }

}

class Persons {
    
    String name;
    Persons friend;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Persons getFriend() {
        return friend;
    }

    public void setFriend(Persons friend) {
        this.friend = friend;
    }
    
    public Persons deepCopy(){
        
        Persons persons = new Persons();
        persons.setName(name);
        persons.setFriend(friend);
        return persons;
    }

}


Output:

Shallow Copy Deep Copy Java

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *