Why does object serialization define the ins and outs of serialversionuid?

Source: Internet
Author: User
Tags object serialization

In many applications, some objects need to be serialized so that they can leave the memory space and stay on the physical hard disk for long-term storage. For example, the most common is the session object in the Web server. When 0.1 million users access the session object concurrently, 0.1 million session objects may appear, and the memory may not be enough, therefore, the Web Container will first serialize some seesion to the memory, and then restore it to the object. To put it bluntly, it will be able to convert a binary file into an object in the memory. In Java, to implement this mechanism, you only need to implement the serializable interface. Let's take a look at the following simple example. The serialversionuid will be introduced later. We first define a simple person class, then create this object, and then serialize it to a file.

Import Java. io. serializable; public class person implements serializable {private string name; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}} import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; public class whyserialversionuid {public static void main (string [] ARGs) throws exception {// The object is serialized to the file person crab = new person (); crab. setname ("mr. crab "); objectoutputstream oo = new objectoutputstream (New fileoutputstream (" crab_file "); oo. writeobject (CRAB); oo. close (); // The object is serialized to the file. comment out the object and use // objectinputstream OI = new objectinputstream // (New fileinputstream ("crab_file ")); // person crab_back = (person) Oi. readobject (); // system. out. println ("Hi, my name is" + crab_back.getname (); // Oi. close ();}}

After running the program, we found a crab_file file that stores the crab object in the memory. Similarly, we comment out this part of code and run the following restoration code. We found that the crab_file file can be converted into an object.

Everything went so smoothly, but what if the person class changed after serialization? For example, a member variable is added. In this experiment, we will first serialize the object to a file, and then add a member variable to the class "person", as shown below:

 

Import Java. io. serializable; public class person implements serializable {private string name; // Add such a member variable private string address; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}

After that, we can run the restore operation again to find an error. The following error will be reported:
Exception in thread "main" java. Io. invalidclassexception: person; local class incompatible: stream classdesc serialversionuid = 8383901821872620925, local class serialversionuid =-763618247875550322
This means that the class in the file stream is incompatible with the class in the classpath, that is, the modified class. In consideration of the security mechanism, the program throws an error and rejects loading. So what if we really need to add a field or method after serialization? What should I do? That is, specify the serialversionuid. Previously, in our example, we did not specify the serialversionuid, so the Java compiler will automatically give this class A Digest algorithm, similar to the fingerprint algorithm, as long as there is one more space in this file, the obtained uid will be completely different. It can be ensured that this number is unique in so many categories. Therefore, after we add a field, because the serialversionuid is not explicitly specified, the compiler generates another uid for us. Of course, this is not the same as the one saved in the file, therefore, two numbers are inconsistent. Therefore, as long as we specify the serialversionuid, we can add a field or method after serialization without affecting the subsequent restoration. The restored object can still be used, you can also use more methods. But how do we generate the serialversionuid? It doesn't matter if you can write 1 or 2, but it is best to generate a unique fingerprint number based on the Digest algorithm. eclipse can automatically generate the number. JDK also comes with this tool. The general syntax is similar
Private Static final long serialversionuid =-763618247875550322l;

 

Note: This article from http://lenjey.iteye.com/blog/513736

Why does object serialization define the ins and outs 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.