What is Java Serialization?

Mondo Technology Updated on 2024-01-30

Q: What is j**a serialization?

A:j**a provides two ways to persist objects, namely serialization and external serialization.

Serialization

In a distributed environment, when communicating remotely, no matter what type of data is transmitted over the network in a binary sequence. Serialization is the process of describing an object as a series of bytes to solve problems that arise when reading and writing to a stream of objects。Serialization can write the state of an object to a stream for network transmission, or save it to a file, database, or other system, and read the stream out to reconstruct the same object when needed.

How do you do this?In fact, all classes to be serialized must implement the Serializable interface, which is located in J**Alang package, it doesn't contain any methods. Use an output stream (e.g. FileOutputStream) to construct an ObjectOutputStream object, and then use the object's WriteObject(ObjectObJ) method to write out the ObjectOutput object (i.e., save its state) and use its corresponding input stream to recover.

Serialization has two characteristics:

1) If a class can be serialized, then its subclasses can also be serialized.

2) Since static represents a member of the class, transient (j**a keyword), if you declare an instance variable with transient, its value does not need to be maintained when the object is stored. ) represents temporary data for objects, so it cannot be serialized as a member of both types of data.

J**A provides multiple interfaces for object serialization, including ObjectOutput, ObjectInput, ObjectOutputStream, and ObjectInputStream.

Because the use of serialization can affect the performance of the system, it should not be used if it is not necessary。So when do you need to use that serialization?

1) The object needs to be sent over the network, or the state of the object needs to be persisted to a database or file.

2) Serialization can achieve deep replication, that is, the referenced object can be copied.

The opposite of serialization is deserialization, which converts a stream into an object. In the process of serialization and deserialization, serialversionuid plays a very important role, each class has a specific serialversionuid, in the process of deserialization, the compatibility of the class is determined by serialversionuid. If the serialversionuid of the object to be serialized is different from the serialversionuid of the target object, an invalidclassexception will be thrown when deserialized. As a good programming Xi, it's a good idea to explicitly declare serialversionuid in the serialized class (this field must be defined as static final). Custom serialversionuid has the following three advantages:

Improve the operational efficiency of the program. If the SerialVersionuid is not explicitly declared in the class, a SerialVersionuid value is computed at serialization time. By explicitly declaring serialversionuid, the process of computation is omitted, thus improving the efficiency of the program.

Improve the compatibility of the program on different platforms。Since it is entirely possible that the compiler of each platform will use different calculations when calculating the serialversionuid, it will be impossible to deserialize objects serialized on one platform on another. This problem can be avoided by explicitly declaring the SerialVersionUid.

Enhance the compatibility of all versions of the program。By default, each class has a unique SerialVersionUid, so when a later modification is made to the class (e.g. by adding a new property), the value of the serialVersionuid of the class will change, which will cause the file that the class had serialized before the modification to be deserialized after the modification. Again, this would be fixed by explicitly declaring SerialVersionuid.

External serialization.

The J**a language also provides another way to implement object persistence, which is external serialization。The interface is as follows:

The main difference between external serialization and serialization is that serialization is built-inapi, only need to implement the serializable interface, the developer does not need to write any ** to achieve the serialization of the object, and when using external serialization, the read and write methods in the externalizable interface must be implemented by the developer。As a result, it is more difficult to write a program using externalizable than it is to implement a serializable interface, but because it puts control in the hands of the developer, there is more flexibility in programming, and the ability to control those properties that need to be persisted may improve performance.

Extension: When serializable is serialized, all properties in this class will be serialized, so how can I serialize only some of the properties?

One method implements the externalizable interface, and developers can implement readexternal and writeexternal methods to control the properties used for serialization and deserialization according to actual needs, which has the disadvantage of increasing the difficulty of programming. Another way to do this is to use the keyword transient to control the properties of the serialization. Properties that are modified by transient are temporary and will not be serialized. Therefore, it is possible to modify properties that do not need to be serialized with transient.

Related Pages