preface

To see is to surrender, which is to say that if you can see something, you can get it.

To me recording is seeing.

The interview questions are not just for the interview, but to help us understand what we already know more deeply.

self-criticism

There are a few more quotes in this article (well, it seems that I have a lot of quotes in each article), please understand, but the quoted article is really what I want, and it is also very good, I hope you can read it and help yourself to understand.

serialization

For this question, I think we need to understand the concept of serialization first. What is serialization? What does serialization do? How do you serialize? Ok, soul Company three asks!

What is serialization (deserialization)

For specific academic level concepts, see the end of this article. Here I use my understanding to briefly describe:

  • Serialization: When we need to put the program into memoryThe heaponNew Person(18," Xiao Ming ")The object is stored, eg as a file on disk.orFor network transport, we must stream the object (that is, turn it into an ordered stream of bytes), a process known as serialization. So why fluidization? Because only objects that are streamed can be transferred, can be stored. Yi? Why is this weird? Okay, fluidized objectsMakes theAs you can see at the end of the reference article below, it is a stream of bytes, so it can be transmitted over the network and stored. I wonder if you made it clear? (You can read itThis articleWhatPart of the).
  • Deserialization: Deserialization is the reverse operation of serialization, which is to serialize the resulting byte stream into our memory objects.

What does serialization do

This function, as mentioned above, is used for network transport and storage.

How to serialize

Two ways:

1. Implement the Serializable interface

2. Implement Parcelable interfaces

Serializable

First of all, this interface comes with Java.

How do you use it? It’s easy, as we all know, to implement this interface for the class we want to serialize (implementing this interface means that our class can be serialized/deserialized), and then use ObjectOutputStream serialization and ObjectInputStream deserialization. (Please refer to this article for details.)

You can read this article about when to use transient and how to use serialVersionUID.

The underlying principle of Serializable, ObjectOutputStream and ObjectInputStream, is what ObjectOutputStream and ObjectInputStream do. You can read Why in this article.

Parcelable

This interface is proprietary to Android. As an extra note, Serializable is used when we need to network transfer or store our objects to files; Use Parcelable(explained at the end) when we need to pass communication between our objects between processes.

Usage: DescribeContents () writeToParcel(Parcel Dest, @writeFlags int Flags); This variable also needs to implement the Parcelable.creator interface.

Before we get to the underlying principles of Parcelable, we need to take a look at the Parcel class, which provides the underlying principles of Parcelable. Here’s how to use and implement a Parcel, and it should be clear how Parcelable is serialized/deserialized underneath.

The difference between

Before we talk about the difference let’s take a look at this SO post.

  1. Parcelable is faster than Serializable(which is the main difference). The explanation for this conclusion can be found in this (must see!). . Well, LET me explain it briefly below:

    A. Severalizable low-level implementation requires reflection (if you don’t know where reflection is used, please look again at the Serializable low-level principle referenced in the WHY section above) and also produces a large number of objects (which may trigger GC); Serializable can also be implemented in IO with ObjectOutputStream and ObjectInputStream.

    The underlying implementation of b.Parcelable does not require reflection, and it is a memory operation. As for why it is a memory operation, besides reading about the use and implementation of Parcel above, I think binder’s implementation or the Activity startup process (which actually uses interprocess communication, binder is used) would be better understood.

  2. The use of Parcelable is more complex than Serializable(I don’t need to explain this).

Ok, finally explain why Parcelable is used for IPC and Serializable for network transfer and save to disk. Let me give you a quote, and then I’ll explain it myself.

  • For IPCParcelableBecause it is efficient, even though we are using itIntentIt can also be used when passingSerializableThe object.
  • Network transfer and save to diskSerializableBecause,ParcelableNo guarantees, donExternal conditionsContinuity of data when changes occur. You can also see the official websiteParcelExplanation:

As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

reference

Cloud.tencent.com/developer/a…