Home  >  Article  >  Java  >  Shallow copy and deep copy in java

Shallow copy and deep copy in java

王林
王林forward
2019-11-28 16:48:321913browse

Shallow copy and deep copy in java

1. What is Java shallow copy?

Shallow copy will copy each attribute of the object in sequence, but when the attribute value of the object is a reference type, what is actually copied is its reference. When the value pointed to by the reference changes, it will also Follow the changes.

2. What is Java deep copy?

Deep copy copies the variable value. For reference data, it recurses to the basic type and then copies it. The deep-copied object is completely isolated from the original object and does not affect each other. Modifications to one object will not affect the other object.

Related video tutorial recommendations: java online video

3. What is the difference between Java shallow copy and deep copy?

Generally speaking, a shallow copy copies its reference, and when the value pointed to by the reference changes, it will also change; while a deep copy is completely isolated from the original object and has complementary effects.

4. Mind map

Shallow copy and deep copy in java

##5. Test case analysis

Shallow copy test case

public class ShallowExperience {
    private String skill;
    public void setSkill(String skill) {
        this.skill = skill;
    }
    public void setShallowExperience(String skill) {
        this.skill = skill;
    }
    @Override
    public String toString() {
        return skill;
    }
}
public class ShallowCloneTest implements Cloneable {
    private int age;
    private ShallowExperience shallowExperience;
    public ShallowCloneTest() {
        this.age = 10;
        this.shallowExperience = new ShallowExperience();
    }
    public ShallowExperience getExperience() {
        return shallowExperience;
    }
    public void setShallowExperience(String skill) {
        shallowExperience.setShallowExperience(skill);
    }
    public void show() {
        System.out.println(shallowExperience.toString());
    }
    public int getAge() {
        return age;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (ShallowCloneTest) super.clone();
    }
}
public class TestMain {
    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("======浅拷贝======");
        shallowCloneTest();
    }
    /**
     * 浅拷贝测试用例
     *
     * @throws CloneNotSupportedException
     */
    private static void shallowCloneTest() throws CloneNotSupportedException {
        ShallowCloneTest test = new ShallowCloneTest();
        test.setShallowExperience("我是小明,我精通Java,C++的复制粘贴");
        test.show();
        ShallowCloneTest cloneTest = (ShallowCloneTest) test.clone();
        cloneTest.show();
        cloneTest.setShallowExperience("我是小明的副本,我精通Java,C++");
        cloneTest.show();
        test.show();
        System.out.println(cloneTest.getAge());
    }
}
//运行结果
======浅拷贝======
我是小明,我精通Java,C++的复制粘贴
我是小明,我精通Java,C++的复制粘贴
我是小明的副本,我精通Java,C++
我是小明的副本,我精通Java,C++
10

Deep copy test case

public class DeepExperience implements Cloneable{
    private String skill;
    public void setSkill(String skill) {
        this.skill = skill;
    }
    public void setDeepExperience(String skill) {
        this.skill = skill;
    }
    @Override
    public String toString() {
        return skill;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class DeepCloneTest implements Cloneable {
    private int age;
    private DeepExperience deepExperience;
    public DeepCloneTest() {
        this.age = 10;
        this.deepExperience = new DeepExperience();
    }
    public DeepExperience getExperience() {
        return deepExperience;
    }
    public void setDeepExperience(String skill) {
        deepExperience.setDeepExperience(skill);
    }
    public void show() {
        System.out.println(deepExperience.toString());
    }
    public int getAge() {
        return age;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        DeepCloneTest deepCloneTest = (DeepCloneTest) super.clone();
        deepCloneTest.deepExperience = (DeepExperience) deepCloneTest.getExperience().clone();
        return deepCloneTest;
    }
}
public class TestMain {

    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("======深拷贝======");
        deepCloneTest();
    }
    /**
     * 深拷贝测试用例
     *
     * @throws CloneNotSupportedException
     */
    private static void deepCloneTest() throws CloneNotSupportedException {
        DeepCloneTest test = new DeepCloneTest();
        test.setDeepExperience("我是小明,我精通Java,C++的复制粘贴");
        test.show();
        DeepCloneTest cloneTest = (DeepCloneTest) test.clone();
        cloneTest.show();
        cloneTest.setDeepExperience("我是小明的副本,我精通Java,C++");
        cloneTest.show();
        test.show();
        System.out.println(cloneTest.getAge());
    }
}
//运行结果
======深拷贝======
我是小明,我精通Java,C++的复制粘贴
我是小明,我精通Java,C++的复制粘贴
我是小明的副本,我精通Java,C++
我是小明,我精通Java,C++的复制粘贴
10

Recommended related article tutorials:

Java zero-based introduction

The above is the detailed content of Shallow copy and deep copy in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete