Shallow copy and deep copy appear in many programming languages, so what is shallow copy and what is deep copy?
To differentiate between shallow and deep copying, we first need to be clear about what replication is and how it is replicated. Examples of replication can also be found in life, such as copying a document, duplicating a piece of text, and so on. We can find that the copy operation can get two copies of the same thing, that is, the copy is changed from one to two. Let's look at an example:
Public class user{ privateint age ; Public int Getage () { return age ; New = user1;
Does the above user2 = User1 belong to copy? Obviously, this operation is not part of replication, because User2=user1 only assigns User1 references to User2, when User1 and User2 point to the same object in memory, so no one becomes two. Replication in Java requires two identical objects, not two identical references.
If you want to get a real copy of the object, you need to implement the Clone () method, the Clone () method exists in the object class, the default is inherited by all classes, the method signature is as follows:
Protected native Object clone () throws Clonenotsupportedexception;
Because the Clone () method is decorated with protected and cannot be called outside the class, if we want to copy an object, we need to overwrite the clone () method in the class and upgrade the modifier to public, and the replicated class needs to implement the Cloneable interface. Otherwise, a Clonenotsupportedexception exception is thrown when the Clone () method is called.
Public classUser implements cloneable{ PublicUser (intAge ) { This. Age =Age ; } Private intAge ; Public intGetage () {returnAge ; } PublicObject Clone () {User User=NULL; Try{User=Super.clone (); }Catch(Exception e) {}returnuser; }}user USR1=NewUser ( A); User USR2=Usr1.clone (); System. out. println (usr1==USR2); System. out. println (Usr1.getage () +", "+usr2.getage ()); ##### #结果false A, A
Indicates that the Clone method has two objects, and the variables are the same. The above example illustrates only the case of shallow copying, and here is another example:
Public classphone{PrivateString name; PublicPhone (String name) { This. Name =name; } Public voidsetName (String name) { This. Name =name; } PublicString GetName () {return This. Name; }} Public classUser implements cloneable{ PublicUser (intAge ) { This. Age =Age ; This. Phone =NewPhone ("Huawei"); } Private intAge ; Publicphone phone; Public intGetage () {returnAge ; } PublicObject Clone () {User User=NULL; Try{User=Super.clone (); }Catch(Exception e) {}returnuser; }}
The code above defines the user class and the phone class, where the user class has a variable for the phone class, and these two classes are called below:
User USR1 =NewUser ( A); User USR2=Usr1.clone (); System. out. println (usr1==USR2); System. out. println (Usr1.getage () +", "+usr2.getage ()); System. out. println (Usr1.phone.getName () +", "+usr2.phone.getName ()); Usr1.phone.setName ("Millet"); System. out. println (Usr1.phone.getName () +", "+usr2.phone.getName ()); # # #结果false A, AHuawei, Huawei Xiaomi, Xiaomi
Have you found any problems? Modify the value of the USR1 variable phone name to "Millet", but the value of the USR2 variable phone name also changes to "millet". This shows that the phone object referenced by USR1 and USR2 's phone variable is the same, the above copy is shallow copy, shallow copy does not copy the reference type of the variable.
To achieve deep replication, we need to make the reference type in the class also replicable, i.e. implement the Cloneable interface, as follows:
Public classPhone implements cloneable{PrivateString name; PublicPhone (String name) { This. Name =name; } Public voidsetName (String name) { This. Name =name; } PublicString GetName () {return This. Name; } PublicObject Clone () {Phone phone=NULL; Try{Phone=Super.clone (); }Catch(Exception e) {}returnphone; }} Public classUser implements cloneable{ PublicUser (intAge ) { This. Age =Age ; This. Phone =NewPhone ("Huawei"); } Private intAge ; Publicphone phone; Public intGetage () {returnAge ; } PublicObject Clone () {User User=NULL; Try{User=Super.clone (); }Catch(Exception e) {} User.phone=(Phone) Phone.clone (); returnuser; }}
From the above can be summarized, shallow copy will be the basic data type of the variables are copied, for reference type variable only to copy the reference, not the reference memory object copy, and the deep copy will copy the object, will also refer to the object's reference variable point to the object copy, deep replication replication more thorough.
The article is posted on the lang degree Cloud website, and the transmission door:
Http://www.wolfbe.com/detail/201608/271.html
Java basics-Shallow copy and deep copy understanding