Definition and use of the Serializable interface in "Android" Android

Source: Internet
Author: User

1 , what does serialization do?

This is simply to preserve the state of various objects in memory (i.e., instance variables, not methods), and to read the saved object state again. Although you can save object states in a variety of ways, Java provides you with a mechanism that should be better than your own to preserve the state of objects, which is serialization.

2 , and under what circumstances need serialization   
A) When you want to save the state of an object in memory in a file or in a database;
b) When you want to use sockets to transfer objects on the network;
c) When you want to transfer objects through RMI;

3. The difference between parcelable and serializable:

Parcelable is recommended for inter-memory data transfer, such as transferring data between activity

Recommended serializable when saving to local or network transfers

Serialversionuid is used to indicate compatibility between different versions of a class.

There are two ways to build: One is the default of 1L, and the other is to generate a 64-bit hash field based on the class name, interface name, member method, and property.

Why you need to overload the Serialversionuid property in a Java class.
When two processes are communicating remotely, each other can send various types of data. Regardless of the type of data, it is transmitted over the network in the form of a binary sequence. The sender needs to convert the Java object to a sequence of bytes to be transmitted over the network, and the receiver needs to revert the byte sequence back to the Java object. The process of converting a Java object to a sequence of bytes is called serialization of an object, and the process of reverting a sequence of bytes to a Java object is called the deserialization of the object.

========================================

SerializableDemo:MainActivity.java saves the data through the serializable interface and jumps to Firstactivity.java, reading the data in Firstactivity.java.

Person class

 Packagecom.example.serializetest;Importjava.io.Serializable; Public classPersonImplementsserializable{Private Static Final LongSerialversionuid = 1L; //member Variables    Private intID; PrivateString name;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }   }

Mainactivity.java

         // Encapsulating Data        New Person ();        P.setid (320840);        P.setname ("lad");         New Intent (mainactivity.  this, firstactivity. class );        I.putextra ("person", p);        StartActivity (i);    

Firstactivity.java

     Person P = (person) getintent (). Getserializableextra ("person");            SYSTEM.OUT.PRINTLN ("id" +p.getid ());     System.out.println ("name" +p.getname ());    

: http://download.csdn.net/detail/xubuhang/8695671

Definition and use of the Serializable interface in "Android" Android

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.