Java I/O---serialization interface serializable

Source: Internet
Author: User
Tags class definition object serialization serialization

Description of the serializable in the 1.JDK API

Class by implementing Serializable interface to enable its serialization functionality. Classes that do not implement this interface will not be able to serialize or deserialize any of their states. All the subtypes of a serializable class are themselves serializable. The serialized interface has no methods or fields and is used only to identify the semantics of serializable.

The serialization runtime is associated with each serializable class using a version number called Serialversionuid , which is used during deserialization to verify that the sender and receiver of the serialized object have loaded a serialization-compatible class for the object. If the serialversionuid of the object's class that the recipient loads differs from the version number of the corresponding sender's class, the deserialization will result InvalidClassException . A serializable class can long explicitly declare its own domain by declaring a field named Serialversionuid, which must be a static (static), final (final) Type field. Serialversionuid:

  1 any-access-modifier static final long serialversionuid = 42L;

If the class can be serialized Serialversionuid is not explicitly declared, the serialization run-time will Calculates the default Serialversionuid value for the class based on the various aspects of the class .As described in "Java (TM) object serialization Specification". ButIt is strongly recommended that all serializable classes explicitly declare Serialversionuid values,The reason is that calculating the default serialversionuid is highly sensitive to the details of the class, and depending on the compiler (version) implementation, it can vary widely, which can cause unexpected results during deserialization InvalidClassException . Soto ensure consistency of serialversionuid values across different Java compilers, the serialization class must declare an explicit Serialversionuid value。 It is also strongly recommended to useThe private modifier displays the declaration Serialversionuid, if possible, because such a declaration applies only to the directly declared class-The Serialversionuid field is not useful as an inherited member. An array class cannot declare an explicit serialversionuid, so they always have a default computed value, but the array class does not have a requirement to match the Serialversionuid value.

        Below is an example:

  1/*  2  * Objects of the person class need to implement the SERIALIZABLE tag interface if serialization is required.  3  * This interface provides a sequence version number for classes that require serialization. Serialversionuid.  4  * The purpose of this version number is to verify that the serialized object and the corresponding class are version-matched.  5  *  6 * * 7 public class person  implements Serializable {  8 9 *  * Declares a sequence version number to the class display.  * * * private static final long serialversionuid = 12324556L; private static String name; + Private transient/ * Transient */int age; Public () () {+ super (); (+) public person  (String name, Int. age) {super (); . name = name; This.age = age; The public  string GetName () {()} () {()} The public void SetName (string name) { = name; () the public int getage () {The return age;} The public void Setage (int.) {PNS This.age = age;  Rride-Public String toString () {[name= ' + ' name + ', age= ' + Age + '] ';  
2.serialVersionUID ( serialized version Uniform identifier) The role of

In Java, software compatibility is a big problem, especially when using the object serialization, then an object has been serialized, but this object has been modified and redeployed, then in this case, using old software to read the new file format is not difficult, However, it is possible to lose some information.

Serialversionuid to solve these problems, the default serialversionuid value is calculated based on the various aspects of the class , which is theoretically a one by one mapping relationship, that is, uniqueness. If the UID is not the same, the serialization cannot be implemented and an exception is obtained InvalidClassException .

the file format defined by the Java serialization mechanism seems fragile, as long as the class definition is changed slightly, the original saved object may not be readable . For example, here is a simple class definition:

  1 public class Save implements Serializable  2 {  3     String name;  4   5 public     Void Save () throws IOException  6     {  7         fileoutputstream f = new FileOutputStream (" Foo ");  8         ObjectOutputStream oos = new ObjectOutputStream (f);  9         Oos.writeobject (this);         oos.close ();     12}

If you add a field to this class definition, such as final int val = 7, and then read the original saved object, the following exception appears:

  1  2 Save; local class incompatible:  3 stream Classdesc Serialversionuid =- 2805284943658356093,  4 local class Serialversionuid = 3419534311899376629

The number string in the exception information above represents the coded values of the various properties in the class definition:
The name of the class (Save).
The first name of the domain (name).
The name of the method (Save).
The implemented interface (Serializable).
Changeany of the above (whether added or deleted) will cause a change in the encoded value, causing similar abnormal alarms. This sequence of numbers is called "serialized version Uniform identifier"(Serial version universal identifier), referred to as UID. The solution to this problem is to add a domain Serialversionuid to the class, forcing the class to still use the original UID. The new domain must be:
Static: The property defined by this field acts on the entire class, not a specific object.
Final: Guarantees that the field will not be modified during code operation.
Long: It is a 64-bit numeric value.
In other words, the displayed serialversionuid must be defined in this form: static final long serialversionuid=-2805284943658356093l;. Where the number followed by the L indicates that this is a long value.


Serialization of JAVA objects (i)--serializable

SERIALVERSIONUID Serialization and use

Java I/O---serialization interface serializable

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: 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.