Serialization of basic concepts of IPC communication

1. Object serialization

    What is object serialization? Looking at the name is a bit abstract. Then the official explanation is: Serialization is the process of converting the state information of an object into a form that can be stored or transmitted. During serialization, objects write their current state to temporary or persistent storage. Later, the object can be recreated by reading or deserializing the state of the object from the store.

   From the above explanation, we can roughly know that serialization is to write the object state into the storage area. Now that the state can be written, the object state can be read from the store and the state can be recreated. Also known as deserialization.

Second, why object serialization

Let's look at an example:

Suppose I have two classes, A and B. Class B contains a reference to an object of class A. Now we instantiate the two classes { A a = new A(); B b = new B() ; }. At this time, two spaces are actually allocated in memory, one for storing object a and one for storing object b. Next we want to write them to a file on disk, then there will be a problem when writing! Because object b contains a reference to object a, the system will automatically copy the data of a to b, so that when we restore the object from the file (that is, reload it into memory), the memory is allocated. There are three spaces, and there are two copies of object a in memory at the same time. Think about the consequences. If I want to modify the data of object a, it is not necessary to search every copy of it to achieve the consistency of object data. Time consuming and memory consuming. This is not ideal.

Therefore, object serialization comes out, which solves this problem very well. It is specifically solved in the following way:

1. All objects saved to disk get a sequence number (1, 2, 3, etc.).
2. When you want to save an object, first check whether the object has been saved.
3. If it has been saved before, just write the tag "same as already saved object with serial number x", otherwise, save the object.

Three, two ways to achieve serialization

So how to implement serialization of objects? There are two main ways, each with advantages and disadvantages. Let's analyze it:

(1) Serializable interface

The Serializable interface is a serialization interface provided by java, but it is an empty interface that provides standard serialization and deserialization operations for objects.

Using Serializable to implement serialization is simple. Just let the class inherit this interface and specify an identifier like the following in the class declaration:

private static final long  serialVersionUID=5651151351613L;

Declaring that this identifier has nothing to do with serialization. Serialization can be successful with or without this identifier, but deserialization may fail. Because deserialization locates the object based on this identifier. If you do not specify this flag manually, then the compiler will automatically generate a hash value of it based on the structure of the current class. But this has a drawback: if the structure of the current class changes, such as adding or deleting some member variables, the system will update its hash value. In this way, there is a problem. This hash value is different from the previously saved hash value, so it will cause the deserialization to fail. But manually specifying a serialVersionUID value can largely avoid the failure of deserialization.

But there is also a special case where manually specifying the serialVersionUID value does not guarantee successful deserialization: that is, if the class structure has undergone unconventional changes: for example, the class name has changed, and the type of member variables has changed. Although the serialVersionUID value is verified, the class structure has undergone destructive changes, and a new class structure object cannot be restored from the old data, so the serialization still fails.

Points to note:

1. Static member variables belong to classes and not objects, so they do not participate in the serialization process.

2. Member variables identified with the transient keyword do not participate in the serialization process.

(2) Parcelable interface

Parcelable is also an interface. As long as this interface is implemented, an object of a class can be serialized and passed through Intent and Binder.

The implementation methods and functions of Parcelable are as follows:

(3) The difference between the two methods

So what's the difference between these two ways?

Compared with Serializable, the usage of Parcelable is more complicated. But it has one advantage: high efficiency.

Serializable is a serialization interface in java. It is simple to use and has a high cost on time. Serialization and deserialization require a lot of I/O operations. But Parcelable is Android's serialization method, which is more suitable for the Android platform. So we prefer Parcelable.

It is possible to serialize the object to the storage device through the Parcelable method or to transmit the object through the network after serialization, but the process is more complicated, so it is recommended to use the Serializable method in both cases.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569137&siteId=291194637