Java serializable and parcelable detailed and instance code _java

Source: Internet
Author: User
Tags int size serialization

Understanding of Serializable and parcelable

1, the first of their two interfaces are to achieve the serialization of objects, so that it can be passed, the so-called serialization is to replace the object information can be stored in the process of media.

2, Serializable is the JDK provided by the serialization interface, the interface exists under the IO package, can be used for input and output, very simple to use, as long as your class to implement this interface OK; You can use the Transient keyword to modify attributes that you do not want to serialize.

3, Parcelable is the SDK provided by the serialization interface, the use of the more troublesome, the implementation of this interface, the need to rewrite the Writetoparcel method, will need to serialize the attributes written into the parcel;

You will then need to cerator the static member Zodiac to remove the data from the parcel. As follows

public static final creator<pen> Creator = new creator<pen> () {
    @Override public
    Pen Createfromparcel (Parcel in) {return
      new Pen (in);
    }
    @Override public
    pen[] NewArray (int size) {return
      new pen[size];
    }
  ;
  @Override public
  int describecontents () {return
    0;
  }
  @Override public
  void Writetoparcel (Parcel dest, int flags) {
    dest.writestring (color);
    Dest.writeint (size);
  }

4, both are used to support serialization, deserialization operation, the biggest difference between the storage medium is different, serializable use IO Read and write stored 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, So in Android it's usually preferred to choose parcelable.

Through this article hope to help the small partners need to thoroughly understand the knowledge of Java serializable and parcelable, thank you for your support to this site!

Related Article

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.