Java basics of object flow (serialization and de-serialization)

A serialization and deserialization:
Serialization: refers to the Java heap memory object data, stored in some way to the object or to a disk file is transmitted to another network node (transmission on the network).
We call this process is called serialization.
deserialized: the object data in a disk file or the object data on a network node, to restore the object to a Java process.

Why do the sequence of:
1): In a distributed system, JavaBean objects need to share data, have to make serialization, then need to re-transmission network on the object, then the object data must be converted to binary form .
after the object is stored in the HttpSession, should implement the serialization interface (only implement serialization interface classes, in order to do serialization).
2): service passivation: If you find that some service activities are not a target for a long time, at this point the server will put these objects in memory, persistent local disk file (Java objects -> binary files).
when the object if some activities need now to look for memory and find on the use, can not be found go to a disk file, we have to deserialize object data recovery into Java objects.

To do serialized object class must implement the serial interfaces: java.io.Serializable interface (the interface flag [No abstract methods]).
Underlying will determine if the current object is the Serializable instance, only allowed to do a sequence of boolean. ret = Java objects instanceof Serializable;

Most of the classes in Java has been achieved Serializable interface.

Second, using the stream of objects to complete the serialization and deserialization operations:
using the object stream is done serialization and deserialization:
the ObjectOutputStream:. To make a sequence of operations by writeObject method
the ObjectInputStream: do deserialize by readObject method .
Here Insert Picture Description

此时报错:User类没有实现序列化接口,java.io.Serializable.

Do deserialization operations bytecode object must exist objects.
Here Insert Picture Description
Three serialization details serialized version:
1): If some data does not need to do serialization, such as passwords, at this time how to do?
In theory, static field and transient field is the sequence of operations can not be done.

2): serialized version of the question:
must provide deserialize Java object class file of the object, the question now is, with the escalation of the project, the class file system will be upgraded (add a field / delete a field), how to guarantee compatibility of two class file? serialVersionUID by the Java (sequence version number) to determine whether the byte-code changes.
If the displayed value is not defined serialVersionUID class variables, variables calculated by the JVM class according to the class information, and calculation of the modified class and before are often different.
thereby causing the problem object deserialization failed because of incompatible versions.

Solution: Provide a fixed serialVersionUID in the class.
Here Insert Picture Description
Four serialization and de-serialization of interfaces and classes.
The Java for the convenience of developers provides a convenient API java object serialization and de-serialization to support, including the following interfaces and classes:
①java.io.Serializable
②java.io.Externalizable
③ObjectOutput
④ObjectInput
⑤ObjectOutputStream
⑥ObjectInputStream

Published 99 original articles · won praise 2 · Views 2594

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105341536