Java deep cloning and shortest cloning

Source: Internet
Author: User

Cloning refers to copying a copy of an object. However, an object may have basic data types, such as int, long, and float. It also contains non-basic data types such as arrays and collections)
The value of the basic type of the cloned object is modified, and the value of the original object is not changed. This method is applicable to Shadow Clone ).

However, if you want to change a non-basic type value, the value of the original object changes ,. for example, if an array only copies its address in memory, but the address points to a value without copy, when cloning, the two addresses point to a value, so once this value changes, of course, the original value also changed, because they share a single value ., this requires deep clone)

The following is an example to illustrate the above situation:

Cloned class: shadowclone. Java

Class Shallowclone Implements Cloneable {
Public   Int A;
Public   Int [] B;

Public Shallowclone (){
A =   5 ;
B =   New   Int [] { 1 , 2 , 3 , 4 , 5 };
}

// For the cloned object, the modification to the basic type members does not affect the corresponding members of the original object.
// For members of the class type and array type, only the address of the object is copied. Therefore, for members of the cloned object
// Modifying will affect the original object.
@ Override
Public Object clone (){
Shallowclone SC =   Null ;
Try {
SC = (Shallowclone) Super . Clone ();
} Catch (Clonenotsupportedexception e ){
E. printstacktrace ();
}
Return SC;
}
}

Test class:

Public   Class Deepandshallowclone {
Public   Static   Void Main (string [] ARGs) Throws Exception {
// Shallow clone
Shallowclone SC =   New Shallowclone ();
Shallowclone sccopy = (Shallowclone) SC. Clone ();


System. Out. println ( " Shallow copy " );
System. Out. println ( " -- Before clone " );
System. Out. println ( " SC. A = "   + SC. );
System. Out. println ( " SC. B = "   + SC. B [ 0 ]);

Sccopy. =   1 ;
Sccopy. B [ 0 ] =   10 ;
System. Out. println ( " -- After clone " );
System. Out. println ( " SC. A = "   + SC. );
System. Out. println ( " SC. B = "   + SC. B [ 0 ]);
System. Out. println ( " Sccopy. A = "   + Sccopy. );
System. Out. println ( " Sccopy. B = "   + Sccopy. B [ 0 ]);
}
}

The result is as follows:

Shallow copy
-- Before clone
SC. = 5
SC. B = 1
-- After clone
SC. = 5
SC. B = 10
Sccopy. = 1
Sccopy. B = 10

The problem occurs. The cloned object sccopy is modified. B [0] value, but SC. the value of B [0] is also changed, with sccopy. the values of B [0] are equal.
The following conclusions are drawn from the light cloning: the basic type can be cloned, but the reference type is only the copy address, and the value of the object pointed to by the copy address is not, this causes the two addresses to point to the same value and change one of them.
It can be seen that the shortest clone is only applicable to the clone basic type, and the clone cannot be implemented for the reference type.

How can we clone referenced objects? The following provides a method to achieve deep Copy Using serialization and deserialization)

Cloned object. deepclone. Java

Class Deepclone Implements Serializable {
Private   Static   Final   Long Serialversionuid =   1l ;
Public   Int A;
Public   Int [] B;

Public Deepclone (){
A =   10 ;
B =   New   Int [] { 6 , 7 , 8 , 9 , 10 };
}

// Use objectinput (output) stream and bytearrayinput (output) stream for deep cloning
Public Object deepclone () Throws Ioexception, classnotfoundexception {
Deepclone DC =   Null ;
Bytearrayoutputstream baos =   New Bytearrayoutputstream ();
Objectoutputstream OOS =   New Objectoutputstream (baos );
Oos. writeobject ( This );
Oos. Close ();

Bytearrayinputstream BAIS =   New Bytearrayinputstream (baos. tobytearray ());
Objectinputstream bis =   New Objectinputstream (BAIS );
DC = (Deepclone) bis. readobject ();
Return DC;
}
}

Test class:

Public   Class Deepandshallowclone {
Public   Static   Void Main (string [] ARGs) Throws Exception {
Deepclone DC =   New Deepclone ();
Deepclone dccopy = (Deepclone) DC. deepclone ();
System. Out. println ( " -- Before clone " );
System. Out. println ( " DC. A = "   + DC. );
System. Out. println ( " DC. B = "   + DC. B [ 0 ]);
Dccopy. =   1 ;
Dccopy. B [ 0 ] =   1 ;

System. Out. println ( " Shallow copy " );
System. Out. println ( " -- After clone " );
System. Out. println ( " DC. A = "   + DC. );
System. Out. println ( " DC. B = "   + DC. B [ 0 ]);
System. Out. println ( " Dccopy. A = "   + Dccopy. );
System. Out. println ( " Dccopy. B = "   + Dccopy. B [ 0 ]);
}
}

The result is as follows:

-- Before clone
DC. = 10
DC. B = 6
Shallow copy
-- After clone
DC. = 10
DC. B = 6
Dccopy. = 1
Dccopy. B = 1

The writeobject method writes out the object relational network of the cloned class, which enables deep cloning. Of course, each cloned member type must implement the serializable interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.