The class version problem in the Java serialization mechanism serialversionuid static field meaning

Source: Internet
Author: User

class version issues in the Java serialization mechanismCategory: "Java Basics"2014-10-31 21:13 480 people read reviews (0) favorite reports

Directory (?) [+]

Original Address: Http://yanwushu.sinaapp.com/java_serialversionuid/Content Introduction

Some Java classes that implement the Serializable interface will see a static field named Serialversionuid, which explains the meaning of this field fundamentally.

Knowledge Cushion

In Java, the serialization and deserialization of a class is implemented by the JVM, and of course, different JVMs may have different implementations, this article discusses the Java official JVM version.

A class that implements the Serializable interface, on behalf of the developer to allow this class to be serialized and deserialized, the JVM obtains the authorization to serialize and deserialize this class. If the serializable is not implemented and the class is serialized directly, the JVM will report an exception.

Serializable is an identity interface, and there are no methods to implement.

Concept

The Java serialization mechanism uses a long field named Serialversionuid to flag the version of the class. When serializing an object, the JVM writes the value of the Serialversionuid to the serialized data of the class, and when deserialized, the JVM compares the Serialversionuid in the object data data with the serialversionuid of the local corresponding class. If the values are not the same (meaning that the versions of the classes are different), then the exception InvalidCastException is reported: The class version does not correspond and cannot be deserialized. If the class version is the same, it can be deserialized.

value

The common serialversionuid values are in two cases:

One is a fixed long value, such as:

Private static final long serialversionuid = 1L;

Private static final long serialversionuid = 123L;

The second is to generate a 64-bit hash based on the class name, interface name, member method, and properties (the IDE can do this), such as:

Private static final long serialversionuid = XXXXL;

When a class that implements the Java.io.Serializable interface does not explicitly define a Serialversionuid value, the Java serialization mechanism generates a SERIALVERSIONUID as a serialized version compared to the compiled class, once the class file is changed (for example, Space, variable name), then Serialversionuid will change as well.

The serialized data is the same as the class version but has a different structure

When a class and its serialized data are the same version (that is, the Serialversionuid value is the same), but the class structure differs, the deserialization process follows the following rules. ---the bottom of the content, there is no content below

The class version problem in the Java serialization mechanism serialversionuid static field meaning

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.