Home  >  Article  >  Java  >  Java uses serialization to achieve deep cloning of objects

Java uses serialization to achieve deep cloning of objects

高洛峰
高洛峰Original
2017-01-18 11:46:041224browse

The example in this article describes how Java uses serialization to achieve deep cloning of objects. Share it with everyone for your reference. The specific implementation method is as follows:

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(obj);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in =new ObjectInputStream(byteIn);
Object newObj=in.readObject();


I hope this article will be helpful to everyone’s java programming.

For more articles on how Java uses serialization to achieve object deep cloning, please pay attention to the PHP Chinese website!

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