Java object serialization and Serialversionuid [reprint]

Source: Internet
Author: User
Tags object serialization

1. Serialization:

Java code

Serialization can transfer a Java object to a network in a binary stream and can be persisted to a database, file system, and deserialization is a Java object that can be re-constructed as a stream of binary data that was previously persisted in a database or file system.

2. The role of serialization

Java code

1 The first: used to store Java object state, usually in a file, so that the next time it needs to be used to read the state information before it.  2 Second: Allows Java objects to be transported over the network.

3. Serialization is the implementation:

Java code

1 1), the class that needs to be serialized needs to implement the Serializable interface, which has no method, just indicates that the class object can be serialized.  2 2), serialization process: Constructs a ObjectOutputStream (object flow) object using an output stream (for example: FileOutputStream), and then Using the WriteObject (Object obj) method of the ObjectOutputStream object, you can write out (that is, save the state) of an object with a parameter of obj3 3), and deserialize the process: using an input stream ( such as: FileInputStream) to construct a ObjectInputStream object, followed by the ReadObject (object obj) of the ObjectInputStream object method to read the object with the parameter obj (that is, to get its state)

4. Static long type constant Serialversionuid effect:

Java code

1 If you do not set this value, after you serialize an object, you change the field of the class or the method name, and so on, if you re-serialize the object you want to remove before you can throw an exception, because you change the information in the middle of the class, Serialversionuid is based on the class name, interface name , member methods and properties to generate a 64-bit hash field, when the modified class is de-serialized, it is found that the Serialversionuid value of the class is inconsistent with the Serialversionuid value previously saved in the Ask price, so an exception is thrown.  234 The Set Serialversionuid value shown will guarantee the compatibility of the version, if you write this value in the class, even if the class is changed, it can also match the original value in the file when it is deserialized. The new value is set to NULL, and the deleted value is not displayed.

5. Example:

Java code

1  PackageCom.sxit;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 ImportJava.io.ObjectInputStream;9 ImportJava.io.ObjectOutputStream;Ten Importjava.io.Serializable; One  A  Public classSerializable01extendsObjectImplementsserializable{ -  -     Private Static Final LongSerialversionuid = -1466479389299512377l; the      -      Public byteb = 12; -      -      Public byteGetb () { +         returnb; -     } +  A      Public voidSETB (byteb) { at          This. B =b; -     } -  -      Public Static voidMain (string[] args) { -          -         Try { inFileOutputStream fos =NewFileOutputStream (NewFile ("E:\\tmp\\demo2.txt")); -ObjectOutputStream OS =NewObjectOutputStream (FOS); toSERIALIZABLE01 s =NewSerializable01 (); +              - Os.writeobject (s); the Os.flush (); * os.close (); $             Panax NotoginsengFileInputStream FIS =NewFileInputStream (NewFile ("E:\\tmp\\demo2.txt")); -ObjectInputStream Ois =NewObjectInputStream (FIS); the              +SERIALIZABLE01 s2 =(SERIALIZABLE01) ois.readobject (); A System.out.println (S2.getb ()); the ois.close (); +              -}Catch(FileNotFoundException e) { $ e.printstacktrace (); $}Catch(IOException e) { - e.printstacktrace (); -}Catch(ClassNotFoundException e) { the e.printstacktrace (); -         }Wuyi     } the}

6. Features of serialization:

Java code

1 1), if a class can be serialized, its subclasses can also, if the class has a parent class, if the parent class implements the Serializable interface, the parent class object field can be serialized, not implemented, the parent class object field cannot be serialized.  2 2) member data declared as transient type cannot be serialized. Transient represents temporary data for an object,3 3), when an instance variable of an object refers to another object, and the object is serialized when it is serialized;

Reprint address : http://luan.iteye.com/blog/1813902

Java object serialization and Serialversionuid [reprint]

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.