Android serializable and parcelable principles and differences

Source: Internet
Author: User
Tags object serialization
<span id="Label3"></p><span id="OSC_h1_1"><span id="OSC_h1_1"></span></span>What is serialization and deserialization?<span id="OSC_h2_2"><span id="OSC_h2_2"></span></span>(1) noun explanation<p><p>Serialization of Objects: the process of converting a Java object into a sequence of bytes and storing it in a storage medium.<br>Deserialization of Objects: the process of reverting a sequence of bytes to a Java object.<br></p></p> <span id="OSC_h2_3"><span id="OSC_h2_3"></span></span>(2) detailed interpretation of serialization<p><p>Serialization of objects involves three point keys: Java objects, byte sequences, storage.<br><br>1. What is the composition of the Java object?<br>Java objects contain variables and METHODS. But sequence and deserialization only handle Java variables without processing methods, and the sequence and deserialization only process the Data.<br><br>2. What is a character sequence?<br>The character sequence is two words, the character is in the computer and telecommunication field, the character (Character) is an information unit. mathematically, a sequence is an object (or event) that is lined up in a column.<br>character-wikipedia, Sequence-wikipedia is plainly a collection of multiple characters that are consecutively Arranged. Similar to 1a165613246546<br><br>3. Storage<br>Character sequences need to be saved to a place, either a hard disk or a memory.<br>The simple argument is that serialization saves the current object Information. Deserializes exactly the opposite Operation.<br><br><br></p></p> <span id="OSC_h1_4"><span id="OSC_h1_4"></span></span>second, What is the difference between a Java object and a Java object serialization?<p><p>The premise of the existence of a Java object must exist during the Jvm's run, and if you want to get the specified Java object in a non-running JVM or on another machine jvm, it cannot be done under the existing Java object Mechanism.<br>Unlike Java objects, if you are serializing a Java object because the principle is to save the Java object information to the storage medium, you can still use the Java object in two cases where the Java object above cannot Exist.<br><br><br></p></p> <span id="OSC_h1_5"><span id="OSC_h1_5"></span></span>third, Why use serialization, deserialization?<p><p>Based on the above understanding of serialization, deserialization, This question can be translated into, why need to save the object information in the storage medium and then read it?<br>Because of the explanation in the second, there is a need to acquire the specified Java object in the case of a JVM that is not running, or on other machine jvms.<br><br><br></p></p> <span id="OSC_h1_6"><span id="OSC_h1_6"></span></span>Iv. What is the difference between serializable and parcelable in Android?<p><p>Both are used to support serialization, deserialization, the biggest difference between the storage medium is different, serializable use IO Read and write storage on the hard disk, and parcelable is directly in memory read and write, it is obvious that the memory read and write speed is usually greater than IO read and write, therefore, parcelable is usually preferred in android.<br>Serializable is not the focus of current attention, but you can view the Java serialization algorithm dialysis article to implement a simple serializable example, view the serialized IO file, and read the meaning of each 16-digit number in 16-binary format.<br><br><br></p></p> <span id="OSC_h1_7"><span id="OSC_h1_7"></span></span>V. Examples of parcelable<p><p>Classes that implement the Parcelable interface in Android can support sequence and deserialization, The following is an example of an implementation:<br>1. Implement the Parcelable interface<br>2. Adding Entity Properties<br>3. Overwrite the writetoparcel (Parcel dest, int Flags) method, specifying the data written to the Parcel class.<br>4. Create Parcelable.creator Static object, There are two methods Createfromparcel (Parcel In) and NewArray (int size), which specifies how to read out the data object from Parcel, which creates an array.<br>5. Overwrite the describecontents method, return 0 by Default.<br></p></p> <pre class="brush:java;"><pre class="brush:java;">public class Gril implements parcelable {private int MAge;//age Private Boolean msexy;//is it sexy @Override          public void Writetoparcel (Parcel dest, int Flags) {dest.writeint (mAge);     Dest.writebyte ((byte) (msexy 1:0)); } public static Final Parcelable.creator</pre></pre> <p><p><br><br></p></p> <span id="OSC_h1_8"><span id="OSC_h1_8"></span></span>Vi. Principles of Parcelable<p><p>As can be seen from the above example, the specific write (dest.writeint (mAge);) and read (gril.mage = In.readint ();) are all operations on the parcel object, and the following is a definition of the parcle read and write int type Data.</p></p> <pre class="brush:java;"><pre class="brush:java;">public final class parcel {    ......         /**     * write an integer  value into the parcel at the current dataposition (),      * growing datacapacity ()  if needed.     */     public final native void writeint (int val);     /**     * Read an integer value from the  Parcel at the current dataposition () .     */     public final native int readint ();          , ...} </pre></pre> <p><p> <br> <br> From the above code can be seen native method description is the use of jni, its specific location in system/frameworks/base/core/jni/android_util_binder.cpp, The following is also only an example of the int type read and write <br> <br> </p></p> <pre class="brush:java;">Static void android_os_parcel_writeint (jnienv* env, jobject clazz, jint val ) {    parcel* parcel = parcelforjavaobject (env, clazz);     if  (parcel != null)  {        const  status_t err = parcel->writeint32 (val);         if  (err != no_error)  {             jnithrowexception (env, java/lang/outofmemoryerror, null);         }    }}static jint android_os_parcel_readint (JNIEnv* env,  jobject clazz) {    parcel* parcel = parcelforjavaobject (env),  clazz);    if  (parcel != null)  {         return pArcel->readint32 ();     }    return 0;} </pre> <p><p><br><br>As you can see from the above, the parcel implementation is called and the WriteInt32 and ReadInt32 functions are called separately, followed by a concrete implementation. Location:/system/frameworks/base/libs/binder/parcel.cpp<br><br></p></p> <pre class="brush:java;"><pre class="brush:java;">status_t parcel::writeint32 (int32_t val) {return writealigned (val);} Template</pre></pre> <p><p><br>The following 4 points are from "exploring the parcel mechanism in Android (above)"<br>Interested friends can read their own, not difficult to understand, here to summarize the basic ideas:<br>1. The whole reading and writing is done in memory, mainly through the memory operations such as malloc (), realloc (), memcpy (), So efficiency is much higher than using external memory in Java serialization;<br>2. Read-write is 4-byte aligned, you can see # define pad_size (s) (((s) +3) &~3) This macro definition is doing this thing;<br>3. If the pre-allocated space is not enough newsize = ((mdatasize+len)/2; It will allocate more than 50% times;<br></p></p> <p><p>4. For normal data, The Mdata memory address is used, the IBinder type of data, and FileDescriptor uses the mobjects memory address. The latter is implemented through Flatten_binder () and Unflatten_binder (), with the goal of deserializing the object that is being read as the original object without having to re-create a new object.</p></p> <span id="OSC_h1_9"><span id="OSC_h1_9"></span></span>seven, serialization of parcelable experiment of deserialization?<p><p>1. Does any entity class require a replication parcelable interface?<br>2. If the subclass new attribute, need to replicate the parent class Writetoparcel and creator?<br>3. Writetoparcel and Createfromparcel to read and write the variables before and after the order can be inconsistent, what results will appear?<br>4. Read and Write Parcelable object (write operation Dest.writeparcelable (obj, flags); Read Operation In.readparcelable (ObjectA.class.getClassLoader ()); )<br>5. Read and write Parcelable object array<br></p></p> <pre class="brush:java;"><pre class="brush:java;">Dest.writeparcelablearray (mclassnamelist.toarray (new classname[mclassnamelist.size ()), flags); parcelable[] Parcelablearr = In.readparcelablearray (ClassName.class.getClassLoader ()); ArrayList</pre></pre> <p><p></p></p><p><p>Android serializable and parcelable principles and differences</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.