The serializable and Parcelable interfaces can accomplish the serialization of objects, and we need two serialization methods when we need to transfer data through intent and binder. Also, we need objects to be persisted to the storage device or transmitted over the network to other clients, and this use also requires the use of Serializale to complete the serialization of the object. Both of these approaches are common in Android app development, but they are not the same in both ways. 1.Serializable interface
The serializable interface is a serialized interface provided by Java, an empty interface that provides standard serialization and deserialization operations for an object. The serialization of objects implemented using serializable is fairly straightforward, and the default serialization process can be automatically implemented by specifying a face reading-like identity in the life of the class.
privatestaticfinallong serialVersionUID=871238749032;
The complete object serialization code example is as follows:
//model Public class User implements Serializable{ Private Static Final LongSerialversionuid=871238749032; Public intUserId; PublicString UserName; PublicString password;}//Serialize to localUser user=NewUser (0,"[email protected]","123456"); ObjectOutputStream out=NewObjectOutputStream (NewFileOutputStream ("User.obj"); Out.writeobject (user); out.close;//DeserializationObjectInputStream in=NewObjectInputStream (NewFileInputStream ("User.obj")); User user= (user) in.readobject (); In.close ();
This approach is a kind of serialization provided by Java, the process is very simple, even some developers do not need to declare SERIALVERSIONUID can also complete the process, but serialversionuid need not specify it?
Need!
Since the Java API provides this serialversionuid, it must be useful. This serialversionuid is used to aid the serialization and deserialization process, in principle the Serialversionuid in the serialized data is only as normal as the serialversionuid of the current class to be deserialized.
The detailed working procedure for Serialversionuid is this: when serializing, the system writes the serialversionuid of the current class to the serialized binary file, When deserializing, the system detects whether the Serialversionuid in the file is consistent with the serialversionuid of the current class, and if consistent, the version of the serialized class is the same as the version of the current class, which can be deserialized successfully Otherwise, the current class and the deserialized class have undergone some changes, such as the number of member variables, the type has changed, this time is not properly deserialized.
In general, we should manually specify the value of the serialversionuid, such as 1L, also allows the IDE to automatically generate its hash value according to the structure of the current class, so that the serialversionuid is the same for both serialization and deserialization. Therefore, the deserialization operation can be performed normally. 如果不手动指定serialVersionUID的值
There are some changes in the current class when deserializing, such as adding or removing some member variables, then the system recalculates the hash value of the current class and assigns it to Serialversionuid. At this point, the serialversionuid of the current class is inconsistent with the serialversionuid in the deserialized data, which results in deserialization failure. So, 手动指定serialVersionUID可以在很大程度上避免反序列化过程的失败。
for example, when the version is upgraded, we may have deleted a member variable and may have added some new member variables, this time our deserialization process still succeeds, the program still can restore the data to the maximum, and conversely, if you do not specify Serialversionuid, The program will occur crash.
Of course, we also need to consider a situation, if the class structure of non-city changes, such as modifying the class name, modify the type of the member variable, this time although the Serialversionuid validation passed, but the deserialization process will still fail, because the structure of the class has a devastating change, There is no way to restore a new class-structured object from the old version of the data.
There are two points to note about using serialization:
1. Static member variables belong to classes that do not belong to the object, so do not participate in the serialization process
2. member variables marked with the Transient keyword do not participate in the serialization process
2.Parcelable interface
The Parcelable interface is an Android SDK-specific way to serialize and deserialize objects in Android apps, with better performance than seriablizable. Objects that implement the Parcelable interface can be serialized and passed through intent and binder.
The following is a completed class that implements the Parcelable interface
Public classUser Implements parcelable{ Public intUserId; PublicString UserName; PublicString password; PublicBook book; Public User(intUserid,string username,string Password,book book) { This. Userid=userid; This. username=username; This. Password=password; This. Book=book; } Public int describecontents(){//returns 0 in almost all cases and returns 1 only if a file descriptor exists in the current object return 0; } Public void Writetoparcel(Parcel out,intFlags) { out. Writeint (UserId); out. WriteString (UserName); out. writestring (password); out. writeparcelable (Book,0); } Public StaticFinal parcelable.creator<user> creator=NewParcelable.creator<user> () { PublicUserCreatefromparcel(Parcelinch){return NewUser (inch); } PublicUser[]NewArray(intSize) {return NewUser[size]; } }Private User(Parcelinch) {userid=inch. ReadInt (); Username=inch. readString (); password=inch. readString (); book=inch. Readparcelable (Thread.CurrentThread (). Getcontextclassloader ()); }}
Looks more complex than the serializable way. We use the form to explain the method of parcelable.
Method |
function |
Mark Bit |
Createfromparcel (Parcel in) |
To create the original object from the serialized object |
|
NewArray (int size) |
Creates an array of original objects of the specified length |
|
User (Parcel in) |
To create the original object from the serialized object |
|
Writetoparcel (Parcel out,int flags) |
Writes the current object to the serialization structure |
Parcalable_write_return_value |
Describecontents |
Returns the content description of the current object, returns 0 in almost all cases, and returns 1 only if a file descriptor exists in the current object |
Contents_file_descriptor |
Since both parcelable and serializable can be serialized and can be used for data transfer between intent, what is the difference between the two?
difference |
Serializable |
parcelable |
Owning API |
JAVA API |
Android SDK API |
Principle |
Serialization and deserialization process requires a lot of I/O operations |
Serialization and deserialization process does not require a large number of I/O operations |
Overhead |
High overhead |
Low Overhead |
Efficiency |
Low |
Very high |
Usage Scenarios |
Serialize to local or over the network |
Memory serialization |
Comparison of two modes of serialization in Android Serializable and parcelable