The role of Serialversionuid

Source: Internet
Author: User

Serialversionuid the serialization mechanism for Java. In simple terms, the serialization mechanism of Java is to verify version consistency by judging the serialversionuid of the class. When deserializing, the JVM compares the serialversionuid in the stream 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. That is InvalidCastException.

Serialversionuid There are two ways to generate the display:
One is the default 1L, such as: private static final long serialversionuid = 1L;
The second 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;

When a class implements the Serializable interface, Serialversionuid,eclipse provides a corresponding reminder if no definition is displayed. In this case, we just need to click the warning icon in the class in eclipse, and eclipse will automatically give you two ways to generate it. If you don't want to define it, you can also turn it off in Eclipse settings as follows:
Window ==> Preferences ==> Java ==> Compiler ==> error/warnings ==> Potential programming problems
Change the warning of the serializable class without serialversionuid to ignore.

When a class that implements the Java.io.Serializable interface does not explicitly define a SERIALVERSIONUID variable, the Java serialization mechanism automatically generates a serialversionuid as a serialized version compared to the compiled class, in which case clas S file (class name, method, etc.) does not change (add spaces, line breaks, add comments, etc.), even if you compile multiple times, Serialversionuid will not change.

If we do not want to compile to force the software version, that is, the entity that implements the serialization interface is compatible with the previous version, it is necessary to explicitly define a variable named Serialversionuid, which is of type long, and serialization entities that do not modify the value of the variable can serialize and crossdress each other.

The following code describes the Serialversionuid in the application of several common situations.

(1) Serialization of entity classes


Import java.io.Serializable;
public class Person implements Serializable
{
    private static final long Serialversionuid = 12345 67890L;
    public int id;
    public String name;
 
    public person (int ID, String name)
    {
         this.id = ID;
        this.name = name;
   }
 
    public String toString ()
    {
         return "Person:" + ID + "" + Name;
   }
}
(2) serialization function:


Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectOutputStream;

public class Serialtest
{

public static void Main (string[] args) throws IOException
{
person person = new person (1234, "Wang");
System.out.println ("person Serial" + person);
FileOutputStream fos = new FileOutputStream ("Person.txt");
ObjectOutputStream oos = new ObjectOutputStream (FOS);
Oos.writeobject (person);
Oos.flush ();
Oos.close ();
}
}
(3) Deserialization function:


Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
public class Deserialtest
{
public static void Main (string[] args) throws IOException, ClassNotFoundException
{
person person;

FileInputStream fis = new FileInputStream ("Person.txt");
ObjectInputStream ois = new ObjectInputStream (FIS);
person = (person) ois.readobject ();
Ois.close ();
System.out.println ("person deserial" + person);
}

}
Scenario One: Assuming that the person class is serialized, it is transmitted from end A to end B and then deserialized on the B-side. When serializing the person and deserializing the person, A and B end require the same class. If the two serialversionuid are inconsistent, what error will be generated?
The "answer" can be tested using the code above to verify:
First execute the test class Serialtest, generate a serialization file, represent a end of the serialized file, and then modify the Serialversion value, and then execute the test class Deserialtest, on behalf of the B end using a different Serialversion class de-serialization, the results of an error:
Exception in thread "main" java.io.InvalidClassException:test. person; Local class Incompatible:stream Classdesc serialversionuid = 1234567890, local class Serialversionuid = 123456789
At Java.io.ObjectStreamClass.initNonProxy (objectstreamclass.java:560)
At Java.io.ObjectInputStream.readNonProxyDesc (objectinputstream.java:1580)
At Java.io.ObjectInputStream.readClassDesc (objectinputstream.java:1493)
At Java.io.ObjectInputStream.readOrdinaryObject (objectinputstream.java:1729)
At Java.io.ObjectInputStream.readObject0 (objectinputstream.java:1326)
At Java.io.ObjectInputStream.readObject (objectinputstream.java:348)
At Test. Deserialtest.main (deserialtest.java:15)

Scenario Two: Assuming that both serialversionuid are consistent, if the a end adds a field and then serializes, and the B end is unchanged and then deserialized, what happens?
"Answer" new public int age; Executes the serialtest, generating a serialization file representing the end of a. Delete the public int age, deserialize, represents the B-end, and the final result is: Perform serialization, deserialization is normal, but the A-side increases the field is lost (ignored by the B-side).

Scenario Three: Assuming two serialversionuid consistent, if the B end of a field, the end of a does not change, what will be the case?
The "Answer" is serialized, the deserialization is normal, the B-terminal field is less than the a end, and the field value of the A-terminal is missing (ignored by the B-side).

Scenario Four: Assuming two serialversionuid consistent, if the B end adds a field, a end is unchanged, what will be the case?
The verification process is as follows:
Execute the Serialtest first, and then add a field of age to the entity class person, as shown below, and then execute the test class deserialtest.


Import java.io.Serializable;
public class person implements Serializable
{
Private static final long serialversionuid = 123456789L;
public int id;
public String name;
public int age;

public person (int ID, String name)
{
This.id = ID;
THIS.name = name;
}

Public String toString ()
{
Return "Person:" + ID + "" + Name;
}
}
The corresponding modification test class deserialtest, prints out the age value.


Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectOutputStream;

public class Serialtest
{

public static void Main (string[] args) throws IOException
{
person person = new person (1234, "Wang");
System.out.println (' person Serial ' + person + ' age: ' + person.age);
FileOutputStream fos = new FileOutputStream ("Person.txt");
ObjectOutputStream oos = new ObjectOutputStream (FOS);
Oos.writeobject (person);
Oos.flush ();
Oos.close ();
}
}
The result is:
Person Deserial person:1234 Wang age:0
Describes serialization, deserialization is normal, and the newly added int field on Terminal B is given the default value of 0.
Finally, through the following picture, summarize the above several cases.

The role of Serialversionuid

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.