Java serialization, Serializable

Source: Internet
Author: User

The Java serialization technique allows you to write the state of an object into a byte stream, and you can read the data in that byte stream from somewhere else and reconstruct the same object. This mechanism allows you to propagate objects through the network, and can persist objects to databases, files and other systems at any time. The serialization mechanism of Java is the technical foundation of RMI, EJB and so on. Purpose: Use the serialization of the object to save the current working state of the application, and will automatically revert to the last state of execution the next time it is started.

Serialization is a mechanism for dealing with the flow of objects, so-called object flow is the flow of the object's contents. It is possible to read and write to a Fluidized object, or to transfer the streamed object between the networks. Serialization is a problem that is raised when reading and writing to an object stream.

Serialization implementation: Implement the Serializable interface for the class that needs to be serialized, and then use an output stream (for example: FileOutputStream) to construct an ObjectOutputStream (object flow) object, and then Using the WriteObject (Object obj) method of the ObjectOutputStream object, you can write out (that is, save its state) the object with the parameter obj, and then use the input stream to recover.

2, the characteristics of serialization:

(1) If a class can be serialized, its subclasses can also be serialized. If the class has a parent class, consider it in two cases if the parent class already implements a serializable interface. The corresponding fields and properties of the parent class are treated the same as the class, and if the parent class of the class does not implement a serializable interface, all field properties of the class's parent class will not be serialized.

(2) member data declared as static and transient types cannot be serialized. Because static represents the state of the class, transient represents the temporary data of the object;

(3) Related classes and interfaces: the classes and interfaces provided in the Java.io package that involve serialization of objects are ObjectOutput interfaces, ObjectOutputStream classes, objectinput interfaces, ObjectInputStream classes.

(1) ObjectOutput interface: It inherits the DataOutput interface and supports serialization of objects, where the WriteObject () method implements the storage of an object. ObjectInput interface: It inherits the Datainput interface and supports serialization of the object, and its ReadObject () method implements reading an object.

(2) ObjectOutputStream class: It inherits the OutputStream class and implements the ObjectOutput interface. This class is used to implement storing objects (calling the WriteObject () method in the ObjectOutput interface). ObjectInputStream class: It inherits the InputStream class and implements the ObjectInput interface. This class is used to implement reading an object (call the ReadObject () method in the ObjectInput interface).

For the processing of the parent class, if the parent class does not implement a serialized interface, it must have a default constructor (that is, a constructor without parameters). Otherwise, you will get an error when compiling. The default constructor is called when the crossdress is being serialized. However, if the parent class is marked as serializable, its default constructor is not called when the crossdress is serialized. What is this for? This is because Java crossdress the serialized object by fetching its object data directly from the stream to produce an object instance, rather than through its constructor.

 Public classCatImplementsSerializable {PrivateString name;  PublicCat () { This. Name = "New Cat"; }     PublicString GetName () {return  This. Name; }     Public voidsetName (String name) { This. Name =name; }     Public Static voidMain (string[] args) {cat cat=NewCat (); Try{FileOutputStream fos=NewFileOutputStream ("Catdemo.out"); ObjectOutputStream Oos=NewObjectOutputStream (FOS); System.out.println ("1>" +cat.getname ()); Cat.setname ("My Cat");            Oos.writeobject (CAT);        Oos.close (); } Catch(Exception ex) {ex.printstacktrace (); }        Try{FileInputStream fis=NewFileInputStream ("Catdemo.out"); ObjectInputStream Ois=NewObjectInputStream (FIS); Cat=(Cat) ois.readobject (); System.out.println ("2>" +cat.getname ());        Ois.close (); } Catch(Exception ex) {ex.printstacktrace (); }    }}//writeobject and ReadObject are thread-safe in themselves and are not allowed to be accessed concurrently during transmission. So the object can be passed on one after the other.

The results of the program run as follows:

1> New Cat
2> My Cat

If the serializable interface is not implemented, the program will error and get the following output information:

1> New Cat
Java.io.NotSerializableException:pa.com.serializable.Cat
At Java.io.ObjectOutputStream.writeObject0 (Unknown Source)
At Java.io.ObjectOutputStream.writeObject (Unknown Source)
At Pa.com.serializable.Cat.main (cat.java:31)
Java.io.WriteAbortedException:writing aborted; Java.io.NotSerializableException:pa.com.serializable.Cat
At Java.io.ObjectInputStream.readObject0 (Unknown Source)
At Java.io.ObjectInputStream.readObject (Unknown Source)
At Pa.com.serializable.Cat.main (cat.java:39)
caused By:java.io.NotSerializableException:pa.com.serializable.Cat
At Java.io.ObjectOutputStream.writeObject0 (Unknown Source)
At Java.io.ObjectOutputStream.writeObject (Unknown Source)
At Pa.com.serializable.Cat.main (cat.java:31)

< Primer:http://blog.csdn.net/yakihappy/article/details/3979373>

Java serialization, 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.