1. Definitions of serializable and Parcelable
1.1 Serializable
package java.io; public interface Serializable { }
Implementation example:
public class Person implements Serializable{ private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } }
1.2 Parcelable
public interface Parcelable { //Content description interface, basically do not care public int describeContents(); //Write interface function, package public void writeToParcel(Parcel dest, int flags); //The purpose of reading the interface is to construct an instance processing of a class that implements Parcelable from Parcel. Because the implementation class is unknown here, you need to use the template method. The inherited class name is passed in through the template parameters //In order to pass in template parameters, the Creator embedded interface is defined here, including two interface functions that return single and multiple inherited class instances respectively public interface Creator<T> { public T createFromParcel(Parcel source); public T[] newArray(int size); } }
Implementation example:
public class Person implements Parcelable{ private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } //The read order here must be consistent with the write order in the writeToParcel(Parcel dest, int flags) method public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); } //The read order here must be consistent with the write order in the writeToParcel(Parcel dest, int flags) method, otherwise there will be errors in the data public Person(Parcel source) { name = source.readString(); age = source.readInt(); } //0 is returned by default @Override public int describeContents() { return 0; } //Write the value to Parcel @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); } public static final Creator<Person> CREATOR = new Creator<Person>() { //It is used by the outer class to deserialize the array of this class @Override public Person[] newArray(int size) { return new Person[size]; } //Read data from Parcel @Override public Person createFromParcel(Parcel source) { return new Person(source); } }; }
The difference between Serializable implementation and Parcelabel implementation
a. For the implementation of serializable, you only need implements Serializable. This just marks the object and the system will automatically serialize it.
b. The implementation of parcelabel requires not only implements Parcelabel, but also a static member variable CREATOR in the class, which needs to implement the Parcelable.Creator interface.
2. Meanings of implements serializable and Parcelabel
2.1 meanings of implements serializable
When does an object need to be serialized?
a. When you want to write objects in memory to your hard disk
For example, if the memory is not enough, the computer should temporarily save some objects in the memory to the hard disk, and then read them into the memory when it is needed. That part of the storage space of the hard disk is the so-called virtual memory. For example, if you want to save a specific object to a file and I use it every few days, then I need to implement the Serializable interface.
b. When you want to use sockets to transfer objects over the network
For example, in Java Socket programming, sometimes you may want to transfer a certain class of objects, so you need to implement the Serializable interface. The most common way is to transmit a string. It is a class in JDK and also implements the Serializable interface. In this way, the data is transformed into binary for transmission, so it can be transmitted on the network.
c. When you want to transfer objects through RMI
For example, if you want to call a remote object by remote method call (RMI), such as calling the object of another computer B in computer A, then you need to get the reference of the B object of the computer through JNDI service, and transfer the object from B to A, so you need to realize serialization interface.
Only when a class implements the Serializable interface can its objects be serialized. Therefore, if you want to serialize the objects of some classes, these classes must implement the Serializable interface. In fact, Serializable is an empty interface with no specific content. Its purpose is to simply identify that the object of a class can be serialized.
For example:
public class Test implements Serializable{
private int id;
private String name;
......
}
If you do not use implements Serializable, the data will be encapsulated and stored in the database with this Test object. When you want to take it out, even if the SQL can correctly return the data in the database, the field contents in the data returned by sessionFactory.getCurrentSession().createNativeQuery(SQL,Test.class).list() are empty (the number of data items is normal and will not be affected), This will cause errors such as method thread 'javax.persistence.PersistenceException' exception.
Summary: the meaning of implements Serializable is to realize serialization and ensure that the data can be retrieved or read completely.
2.2 meanings of implements parcel
The meaning of implements parcel is the same as that of implements Serializable. Both implement serialization. The differences are as follows:
a. When using memory, Parcelable has higher performance than Serializable, so it is recommended to use Parcelable.
b. Serializable generates a large number of temporary variables during serialization, resulting in frequent GC.
c. Parcelable cannot be used when the data is to be stored on disk, because Parcelable cannot guarantee the continuity of the data in case of external changes. Although Serializable efficiency is low, it is recommended to use Serializable at this time.
Summary:
Both Serializable and Parcelable are implemented for serialization. The difference between them is reflected in two places:
a. The implementation of the Serializable interface is very simple. Just declare it; However, to implement Parcelable, you need to override the writeToParcel method, override the describeContents method, instantiate the static internal object CREATOR, and implement the interface Parcelable.Creator
b. When using memory, Parcelable has higher performance than Serializable, so it is recommended to use Parcelable.
Good article:
https://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html
https://blog.csdn.net/am540/article/details/82498298