What is java serialization, how to achieve java serialization? Please explain the role or Serializable interface.

It must first clear the purpose of serialization:

    1. Regardless of the type of data in binary form are transmitted over the network, in order to process transmitted by a Java object to another process, it is necessary to convert it to a sequence of bytes transmitted on the network, the JAVA object process for the conversion sequence of bytes is called a serialized object, the recovery sequence of bytes into Java objects is called object deserialization, (java.io.ObjectOutPutStream then the writeObject (object obj) method may the parameters specified object serialized byte stream and the resulting stream is written to a target output up)

 

2. Objects can only achieve a serializable and Externalizable interface class is serialized latter is the former subclass implementation of this class entirely by itself excuse to control the sequence of behavior, but merely implements the default sequence of the former class can be used of the way. Implementing these interfaces marked objects can be serialized. . .
Summary
Java serialization technique allows you to write the state of an object in a Byte stream, and can be read from the stream elsewhere in the Byte data out of a re-configuration of the same object.

Java serialization technique allows you to write the state of an object in a Byte stream, and can be read from the stream elsewhere in the Byte data out of a re-configuration of the same object. This mechanism allows you to target spread through the network, and may at any time object persistence to the database, file, etc. system. Java serialization mechanism is the technical basis RMI, EJB and other technologies. Uses: the use of serialized objects to achieve the current operating state of preservation of the application, the next time will restart automatically restored to the state last executed.

It is a kind of serialization mechanism for an object a process stream, that is, so-called content object stream object to be fluidized. After the fluidized objects can read and write operations, the object can also be transmitted after fluidizing between networks. Serialization is to solve the problem when the stream of objects to read and write operations initiated.

Serialization implementation: will need to be serialized class implements Serializable interface, then an output stream (eg: a FileOutputStream) to construct a ObjectOutputStream (object stream) object, then, using an ObjectOutputStream object writeObject (Object obj) method can be used the parameters for the object obj to write (i.e., to save its state), to be restored if the input stream.

2, serialization features:

    (1) if a class can be serialized, subclasses may be serialized. If the class has a parent class, the two cases to consider, if the parent class has been achieved may be serial interfaces. And processing the same class and its parent class attribute corresponding field; if the class' parent class does not implement the serialization interface can be, for all the parent class of the class field properties will not be serialized.

  (2) declared transient and static data members of the type can not be serialized. Because the static state representative of the class, transient temporary data representative of the object;

  (3) the relevant classes and interfaces: providing in serial java.io package classes and interfaces of the object are directed ObjectOutput interfaces, ObjectOutputStream classes, ObjectInput interfaces, ObjectInputStream class.

    (1) ObjectOutput interfaces: it inherits the interface and supports DataOutput serialized objects, writeObject therein () method to store an object. ObjectInput interfaces: it inherits DataInput interfaces and supports serialized objects, readObject therein () method reads an object.

    (2) ObjectOutputStream class: OutputStream class that inherits the interface and implement ObjectOutput. To achieve the object store (call writeObject () method ObjectOutput interface) using the class. ObjectInputStream class: InputStream class that inherits and implements ObjectInput interface. To achieve an object is read (called an ObjectInput interface readObject () method) using class.

  For the treatment of the parent, if the parent class does not implement the serialization interface, it must have a default constructor (i.e., no parameters constructor). Otherwise, it will compile time error. In deserialization when the default constructor is called. However, if the parent class can be marked for serialization, deserialization at the time, the default constructor will not be called. Why is this? This is because the Java object serialization on deserialization time, directly from the data stream in which the object to generate an object instance, rather than complete through its constructor.

Guess you like

Origin www.cnblogs.com/ldddd/p/11414023.html