java object serialization parse

Why JAVA objects need to implement serialization?

  • Serialization for a 处理对象流mechanism.

  • The so-called stream of objects: that is, the content object to stream. After the object can be fluidized 进行读写操作objects later, it may also be fluidized 传输于网络between.

  • 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 implementation Serializable接口(标记接口), the method of the interface does not need to achieve, implements Serializable only to 标注the object is serialized, then an output stream (eg: a FileOutputStream) to construct a the ObjectOutputStream ( stream objects) object; Next, ObjectOutputStream object writeObject (object obj) method can be used to write the object obj parameters (i.e., to save its state), to be restored if the input stream.

  • When using a sequence of it?

    • A: object serialization can 实现分布式对象.
      Main applications such as: RMI(即远程调用Remote Method Invocation)To use the services running on the remote host object serialization, just as when an object is running on the local machine.
    • Two: java object serialization is not only an object's data retention, and recursive data for each object stored object reference.
      Entire object hierarchy can be written in the byte stream, or may be stored in a file transfer over a network connection. Target sequence can be carried out using object " deep copy ", that is 复制对象本身及引用的对象本身. Object may obtain a sequence of the entire target sequence.
    • Three: the sequence of classes in memory can be written in a file or database.
      For example: a class after the serialization save a file, just the data in the file the next time reading 反序列化can be restored to the original class in memory. Like sequences also can be transmitted into a data stream. Overall is 将一个已经实例化的类转成文件存储, next time you need to instantiate a long time deserialization can instantiate the class into memory and keep all the variables and status class serialization.
    • Four: objects, files, data, there are a number of different formats, it is difficult unified transport and storage.
      After we are all serialized byte stream, no matter what the original thing, can 变成一样的东西, can be a common format for transmission or saved, after the end of transmission, to be used again, it is deserialized reduction, so that the object or objects, file or file
  • Because you want to target sequences in JAVA into  流的形式 transmission.


  • Serialized object is to transfer data in your code where object format, and at the time of transmission can not remain like this also object.
  • When the two remote communication process is performed, various types of data can be transmitted each other. No matter what type of data will be in the form of a binary sequence transmitted on the network. The sender need to convert the Java object is a sequence of bytes to be transmitted over the network; recipient need to take a sequence of bytes and then return to Java objects.
  • 1. Concept
    • Serialization: The process of converting a Java object byte sequence.
    • Deserialized: the recovery process for the byte sequence of Java objects.
  • 2. The use of a sequence of objects with two main purposes:
    • 1) The target byte sequence permanently saved to the hard disk, usually stored in a file.
    • 2) target sequence of bytes transmitted on the network.

  • The so-called Serializable, is the universal data provided by java stored and read interface. As read out from somewhere and save Where are hidden behind the argument of the function. This way, so long as any type of implements Serializable interface, it can be saved to a file, or transmitted as a data stream over the network to another place. Pipe may be transmitted to other programs in the system. Like this greatly simplifies the design of the class. As long as a design feature can save a reading above right solve all the problems.
  • java "Object Serialization" will make you a target to achieve a conversion Serializable interface to a set of byte, so that in the future to use this object, you will be able to restore the data byte out, and accordingly the object of the rebuild.
  • Among several data workflow process variables types: string, integer, short, long, double, boolean, date, binary, serializable, which is why you want to achieve javabean reason for serialization, because you set the object to process variables We must implement serialization, otherwise it will not find the type of error in the setup process variable times.
  • java object serialization mechanism is to Java objects in memory (JavaBean User like) converted into a binary stream. After java object serialization can be easily stored or transmitted in the network.
  • Java serialization mechanism is determined by run-time version of the consistency of the sequence ID (serialVersionUID) determining the class.
  • When deserializing, java virtual machine via a binary stream serialVersionUID local entity class corresponding comparison, if the same is considered to be the same, it can be deserialized properly obtain information, or throw inconsistent serialized version exception.
  • 所以涉及到数据传输或者存储的类,严格意义上来说都要加上序列化ID,这也是一种良好的编程习惯

 

 

Another good article below, there are specific examples:

Java object serialization and de-serialization

 

 

Published 118 original articles · won praise 59 · views 490 000 +

Guess you like

Origin blog.csdn.net/u012255097/article/details/102540543