No poetic female program apologise not good cook ~ reprint please indicate the source, the From Li Shiyu – blog.csdn.net/cjm24848365…

First, I will force a graph, hahahahahaha ~

Interviewer: “Tell me the difference between Serializable and Parcelable.”

We can say ~ from the following two aspects

First of all, let’s give them a brief introduction:

Definition 1.

  • Serializable (Java built-in) :

    Serializable stands for serialization, which means converting an object to a state that can be stored or transferred. Serialized objects can be transferred over the network or stored locally.

  • Parcelable (Android only) :

    In addition to Serializable, the same effect can be achieved using Parcelable,

    But instead of serializing an object, the Parcelable approach works by shredding an entire object,

    Each part of the decomposition is the data type supported by the Intent, which is used to pass objects.

Of course, this is still a little too little to say, not in depth, and not to delay the effect, so we can also continue to chatter from the following point:

2. Principle of selecting serialization method:

Since both are used for serialization, how do we choose? The criteria are as follows:

  • Use Parcelable in memory

    The performance of using Parcelable in memory is high. The main reason is that Parcelable decomposes an object, and every part of the decomposed content can be supported by intent.

    Serilalizable is more binary for serialized actions.

    And Serializable generates a large number of temporary variables when serializing, which can cause frequent GC operations,

    So when we pass an object from Activity A to Activity B in memory, for example, using Serilalizable, there are lots of temporary variables that can degrade performance.

    So, in memory, whenever we’re transferring objects, we’re using Parcelable.

  • Serializable is used for persistence

    Serializable takes up less storage space than Parcelable during persistence because it is directly converted to our binary form. Serializable also does not fail to be deserialized due to the addition or subtraction of fields in an object, so it is recommended to use Serializable for persistence, and it is stored in a file and takes up less disk space.

Accumulate drip, do yourself ~