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:
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!