Home >Java >Javagetting Started >What is the difference between deep copy and shallow copy in java

What is the difference between deep copy and shallow copy in java

王林
王林forward
2020-07-04 16:58:343354browse

What is the difference between deep copy and shallow copy in java

Difference analysis:

(Recommended learning: java entry program)

Shallow copy

All variables of the copied object contain the same values ​​as the original object, and references from all other objects still point to the original object.

In other words, a shallow copy only copies the object under consideration, not the objects it refers to.

Deep copy

All variables of the copied object contain the same values ​​as the original object, except those variables that refer to other objects. Variables that reference other objects will point to the copied new object, not the original referenced objects.

In other words, deep copy copies all the objects referenced by the copied object.

(Video tutorial recommendation: java video tutorial)

Define a class as follows:

class Test {
    public int i;
    public StringBuffer s;
}

The following figure shows the copying of objects of this class When, the difference between shallow copy and deep copy:

What is the difference between deep copy and shallow copy in java

The above is the detailed content of What is the difference between deep copy and shallow 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