About the serialversionuid of serializable

Source: Internet
Author: User

in the class that implements the Serializable interface, you need to declare a long serialversionuid, which isused to indicate The version number of the current class. But many people in programming, always do not want to declare this serialversionuid, and the JVM itself to generate. Here's a look at the role of Serialversionuid:

1. If the version number at the time of serialization and the version number of the serialized read is inconsistent, there will be an exception: Java.io.InvalidClassException:local class Incompatible:stream Classdesc Serialversionuid = ..., local class Serialversionuid = ...

2. What if this attribute is not declared in class? The result can be more bizarre:

1), in the serialization of the time, the virtual machine a will calculate a serialversionuid for it , thecalculation method is based on class information, and then specifically I am not clear.

2), when serializing the read, virtual machine B will also calculate a Serialversionuid for class , and then do the comparison.

3), then if two virtual machines are different types of virtual machines, then the calculation method may not be the same, so even if the same class,serialversionuid may be different, different class , Theoretically, there is also the possibility of serialversionuid the same, soSerialversionuid as far as possible by our own to specify, and not by the virtual machine to calculate.

3, if the serialversionuid is consistent, and the class has changed it?

1), if AClass in virtual machine a has a property, and AClass in virtual machine B does not have this attribute, then this property will be ignored without exception.

2), if the aclass in virtual machine A does not have an attribute, and the attribute is more in virtual machine B, then this property will be given a default value without exception.

3 "If Virtual machine a< Span style= "font-family: the song Body;" The aclass There is a property in the virtual machine baclassint became long

The exception generated by the serialization is java.io.InvalidClassException, does not produce java.lang.ClassCastException, the two still have a relatively big difference, It can be seen from the name.

Will you also let the JVM generate Serialversionuid itself? Do you also add @suppresswarnings ("Serial")? It may not be possible to see any problems on a single machine, but this problem will be exposed when distributed computing, or when you need to provide jars for others to use.

About the serialversionuid of serializable

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.