5

After some time using the implementation Serializable in my classes on Java (Android) I discovered the Parcelable, but I couldn't find out when to choose one or another. Also what are the performance differences between them?

Michael Krause
  • 4,225
  • 1
  • 18
  • 23
Blanco
  • 105
  • 1
  • 4
  • For Parcellable check this link. :- http://stackoverflow.com/a/7181792/2362301 For Seriallizable checkout this link:- http://www.coderzheaven.com/2012/07/25/serialization-android-simple-example/ – AmmY Sep 15 '16 at 18:53
  • 2
    Did you even *try* to Google [`Parcelable Vs. Serializable`](https://www.google.com/search?q=Parcelable+Vs.+Serializable)? – Andreas Sep 15 '16 at 19:00

3 Answers3

11

what are the performance differences between them?

Parcelable is signficantly faster than is Serializable for the places where you use Parcelable: Intent extras, saved instance state Bundle, etc.

That being said, assuming that you are not doing lots of this stuff, particularly in tight loops, users are unlikely to really notice the difference.

when to choose one or another

If you are doing Android development, prefer Parcelable to Serializable where Parcelable is an option. At most, use Serializable for data persistence, though I would recommend other serialization options even for that (e.g., JSON via Gson).

The one primary exception to this would be if your Java objects are in a separate library that would be used both from Android and from other Java environments. Those environments will not have Parcelable, but Serializable would work in both places.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
3

Parcelable assists in passing data between Android components. Binder marshals the Parcel to be sent, sends and receives it, and then unmarshals it on the other side to reconstruct a copy of the original Parcel.

w.r.t Serializable you only need to implement the Serializable interface on a class and its children. It is a marker interface, meaning that there is no method to implement, Java will simply do its best effort to serialize it efficiently.It uses Reflection that tends to create a lot of temporary objects and cause quite a bit of garbage collection.

Per the google engineers Parcelable is more than 10x faster than Serializable. In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization.

You may check sample code here to understand the implementation difference- Android: Difference between Parcelable and Serializable?

Be Careful?

  • Pay close attention to is the order that you write and read your values to and from the Parcel.The mechanism that Android uses to read the Parcel is blind and completely trusts you to get the order correct, or else you will run into run-time crashes.
  • There is no simple way to write a boolean to a Parcel. To do so, you can instead write a byte with the corresponding value with out.writeByte((byte) (myBoolean ? 1 : 0)); and retrieve it similarly with myBoolean = in.readByte() != 0;

Check the following projects to avoid writing Parcelable manually.

Community
  • 1
  • 1
Krunal
  • 93
  • 4
0

Parcelable is better for android: 1. It's faster 2. It doesn't use reflection (Serializable uses Reflection)

alla
  • 500
  • 3
  • 19