Serialization and deserialization of Java objects

This example shares the serialization and deserialization of Java objects for your reference. The specific contents are as follows

1. What is serialization? Convert an object into a byte stream and save it, for example, to a file, and restore the object later. This mechanism is called object serialization. (add: saving an object to a permanent storage device is called persistence)

2. How to realize serialization needs to implement the serializable interface. The Java object implements this interface, which indicates that the object of this class is serializable.

3. Precautions for serialization

(1) When an object is serialized, only non static member variables of the object can be saved, but methods and static member variables cannot be saved. (2) object a references object B, object a is serialized, and B is serialized. (3) If a serializable object contains a reference to a non serializable object, the whole serialization operation will fail, and the failure will throw notserializableexception. Therefore, the serializable interface must be implemented for its own object and reference object. (4) With the use of transient, if the member variable or reference is marked transient, the object can still be sequenced, but it will not be serialized into the file.

4. Code

Output result: --------- zhangsan-0 lisi-0 5 Finer grained control of serialization and deserialization

When we implement the above two private methods in the serialized or deserialized class (the method declaration should be completely consistent with the above), it allows us to control the process of serialization and deserialization in a lower level and more successive granularity.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>