Java Serialization (Serializable) and deserialization

Source: Internet
Author: User

What does serialization do?

This is simply to preserve the state of various objects in memory (i.e., instance variables, not methods), and to read the saved object state again. Although you can save object states in a variety of ways, Java provides you with a mechanism that should be better than your own to preserve the state of objects, which is serialization.

What happens when serialization is required
    1. When you want to save the state of an object in memory in a file or in a database;
    2. When you want to use sockets to transfer objects on the network;
    3. When you want to transfer objects through RMI;
Several ways to serialize

Data types are often difficult to choose when transmitting data in the socket in Java. You may want to consider issues such as bandwidth, cross-language, version compatibility, and so on. There are two common approaches: one is to wrap the object into a JSON string transfer, and the other is to use the serialization and deserialization of the Java object. With Google tools Protobuf Open source, Protobuf is also a good choice. Make a comparison between the Json,object serialize,protobuf.

Object Serialize

The serialization mechanism for Java is to validate version consistency by judging the serialversionuid of the class at run time. When deserializing, the JVM compares the serialversionuid in the stream that is transmitted to the serialversionuid of the local corresponding entity (Class), and if the same is considered consistent, it can be deserialized, otherwise the serialized version inconsistency exception will occur.

Serialversionuid is used to indicate compatibility between different versions of a class. There are two ways to build:

    1. One is the default 1L, for example: private static final long serialversionuid = 1L;
    2. One is to generate a 64-bit hash field based on the class name, interface name, member method, and property, for example: private static final long serialversionuid = XXXXL;

The following is a discussion of why the Serialversionuid property needs to be overloaded in Java classes?
When two processes are communicating remotely, each other can send various types of data. Regardless of the type of data, it is transmitted over the network in the form of a binary sequence. The sender needs to convert the Java object to a sequence of bytes to be transmitted over the network, and the receiver needs to revert the byte sequence back to the Java object.

    1. The process of converting a Java object to a sequence of bytes is called serialization of an object.
    2. The process of reverting a sequence of bytes to a Java object is called deserialization of the object.

There are two main purposes for serialization of objects: (1) to permanently save the byte sequence of the object to the hard disk, usually in a file, and (2) to transmit the byte sequence of the object on the network;

Java.io.ObjectOutputStream represents an object output stream, and its writeobject (object obj) method serializes the Obj object specified by the parameter and writes the resulting sequence of bytes to a target output stream.

Java.io.ObjectInputStream represents an object input stream, and its readobject () method reads a sequence of bytes from a source input stream, deserializes them into an object, and returns them.

Only objects of classes that implement the serializable and Externalizable interfaces can be serialized. The Externalizable interface inherits from the serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, whereas classes that implement the serializable interface can take the default serialization method.

Any class that implements the Serializable interface has a static variable that represents the serialized version identifier: private static final long serialversionuid;

The default value of the Serialversionuid class is entirely dependent on the implementation of the Java compiler, and compiling with different Java compilers for the same class may lead to different serialversionuid and possibly the same. To improve the independence and certainty of serialversionuid, it is strongly recommended that the definition serialversionuid be displayed in a serializable class, giving it a definite value. There are two ways to explicitly define SERIALVERSIONUID:

    1. In some cases, you want different versions of the class to be serializable compatible, so you need to ensure that different versions of the class have the same serialversionuid, and in some cases you do not want different versions of the class to be serializable compatible, so you need to ensure that different versions of the class have different serialversionuid.
    2. When you serialize a class instance and want to change a field or add a field without setting serialversionuid, any changes will result in the inability to deserialize the old instance and throw an exception when deserializing. If you add serialversionuid, the newly added or changed field value will be set to the initialization value (the object is null, the base type is the corresponding initial default value) when the deserialization is old, and the field is deleted without setting.
JsonGoogle Protobuf

Java Serialization (Serializable) and deserialization

Related Article

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.