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

王林
Release: 2020-07-04 16:58:34
forward
3349 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template