Deep copy of Java objects----deep replication with serialization

Source: Internet
Author: User

The process of writing an object into a stream is a serialization (serilization) process, but in the Java programming Circle it is also very visually called a "frozen" or "pickled pickle" process, while the parallelization (deserialization) process of reading an object from the stream is called "thawing" or "fresh". Process. It should be noted that a copy of the object is written into the stream, and the original object still exists inside the JVM, so "pickle" is just a copy of the object.

In the Java language, a deep copy of an object can often be made by implementing the Serializable interface, and then the object (actually a copy of the object) is written into a stream (pickled into pickles) and then read back from the stream to reconstruct the object.

public class Deepcopydemo implements Serializable{public Object Deepclone () throws exception{// Write the object into the stream Bytearrayoutputstream bo=new bytearrayoutputstream (); ObjectOutputStream oo=new ObjectOutputStream (bo); O O.writeobject (this);//Read the object out Bytearrayinputstream bi=new bytearrayinputstream (Bo.tobytearray ()); ObjectInputStream Oi=new ObjectInputStream (BI); return (Oi.readobject ());}}

Deep copy of Java objects----deep replication with serialization

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.