Serialversionuid role of serialization in Java

Source: Internet
Author: User

Java serialization is the encoding of an object into a byte stream, and deserialization converts the byte stream encoding into an object. Serialization is a method of implementing persistent storage in Java and provides a line-level object representation for data transfer.

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.

Eclipse in the Serializable class XXXXXX does not declare a static final serialversionuid field of type long appears such a warning handling method.
When using the program's add default Serial version ID fix, Eclipse adds: private static final long serialversionuid = 1L;
When using the program's add generated Serial version ID fix, Eclipse adds: private static final long serialversionuid = XXXXL;

In fact, the specific reason for this problem is related to this serialversionuid in serialization. Serialversionuid is used to indicate compatibility between different versions of a class. There are two ways to build: One is the default of 1L, and the other is to generate a 64-bit hash field based on the class name, interface name, member method, and property.
In the JDK, you can use the Serialver.exe tool under the Bin directory of the JDK to generate this serialversionuid value, and for Test.class, execute the command:

Serialver Test the JVM (Java Virtual machine) generates a hash field.

Comparing the value of this hash field to the field value generated in Method 2, it is visible that using the serialver directive in CMD generates a hash field based on the class name, interface name, member method, and property.

Why you need to overload the Serialversionuid property in a Java class.
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. The process of converting a Java object to a sequence of bytes is called serialization of an object, and the process of reverting a sequence of bytes to a Java object is called the deserialization of the object.

There are two main uses for serialization of objects:
1) The object's byte sequence is permanently saved to the hard disk, usually stored in a file;
2) A sequence of bytes that transmits an object over 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 a class that implement the serializable or Externalizable interface 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, which, for the same class, is compiled with different Java compilers, which can lead to different serialversionuid. There are two ways to explicitly define SERIALVERSIONUID:

1) On some occasions, 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. It is therefore necessary 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.

The serialization algorithm generally follows the steps of:

    1. Outputs the class metadata related to the object instance.
    2. Recursively outputs a superclass description of a class until there are no more super-classes.
    3. After the class metadata is finished, start outputting the actual data values of the object instances from the topmost superclass.
    4. Recursive output of data from top to bottom instances

Serialversionuid role of serialization in Java

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.