Home » Android » serializable – what is difference between Parcelable and Serialization used in android

serializable – what is difference between Parcelable and Serialization used in android

Posted by: admin April 23, 2020 Leave a comment

Questions:

I want to know exact ,

  1. whether should I used parcelable or serialization technique
    for sending data from one activity to other?
  2. is it compulsory to
    use one of them for sending data from one to other?
  3. when should I use them?
  4. and the exact difference between them and performance
    of both of them in java aspects.

Thanks in advance.


 public class GetSetClass implements Serializable {
    private int dt = 10;

    /** pass any object, drwabale */
    public int getDt() {
        return dt;
    }

    public void setDt(int dt) {
        this.dt = dt;
    }
}
How to&Answers:

whether should i used parcelable or serialization technique for sending data from one activity to other.

If you are sending a non-primitive type data/Object to another activity through the intent you have to either Serialize or implement Parcelable for that object. The preferred technique is Parcelable since it doesn’t impact the performance.

is it compulsory to use one of them for sending data from one to other. / when should i use them.

It is only compulsory/used for sending non-primitive type data objects.

and the exact difference between them and performance of both of them in java aspects.

Serialization does impact the performance. For more details check this link Android Parcelable and Serializable

Answer:

These concepts are related to Inter Process Communication (IPC).

When sending data between two applications, we have to make sure that both applications should understand the format of the data that is being sent.

Especially when you are sending non primitive data type values like classes and objects between two applications, We have to convert them into Operating System understandable format. O.S understands only primitive types (ints, chars etc). The reason for conversion is we have to O.S communication channel to transmit the data.

This process of converting Non primitive types to primitives and sending across to other application over some communication channel is called as Serialization. The reverse process is called as De Serialization.

In Java, IPC depends heavily on Serializables for serialization. But serialization is designed by keep desktop applications in mind. When you are doing IPC in mobile applications we have to make sure that the process of IPC is not too heavy.

In simple terms serialization is a heavy concept for IPC.
So in place of Serialization Android opted for Binders for achieving light weight Inter process communication. Binders internally depends heavily on parcels, to do the IPC.
Parcels are light weight serializables. It is preferred to use parcels for marshaling objects into byte streams.

Note: Binder IPC heavily depends on Shared memory concept to make sure that there is not much data duplication while sharing between applications.

Answer:

Got a very good explanation of difference between Parcelable and Serialization.

To start with your question though its been a long time, it may help others:

  1. whether should I used parcelable or serialization technique for sending data from one activity to other?

Ans: Parcelable is best choice (why explained later).

  1. is it compulsory to use one of them for sending data from one to other?

Ans: Yes, as sending data (object) from one to other requires streams of bytes to be written and retrieved and that can be done either through parcelable or serialization.

  1. when should I use them?

Ans: This part you arleady answered i.e, passing data from one activity to another.

  1. and the exact difference between them and performance of both of them in java aspects.

Ans: 1. Parcelable is well documented in the Android SDK; serialization on the other hand is available in Java.

  1. In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.

  2. Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparison to Parcelable.

Answer taken from:
this link

See also:serialization explained

Answer:

Java Serializable:-
Serializable comes from standard Java and is much easier to implement all you need to do is implement the Serializable interface and add override two methods.

The problem with Serializable is that it tries to appropriately handle everything under the sun and uses a lot reflection to make determine the types that are being serialized. So it becomes a beefy Object.

Androids Parcelable:-
Android Inter-Process Communication (AIPC) file to tell Android how is should marshal and unmarshal your object.It is less generic and doesn’t use reflection so it should have much less overhead and be a lot faster.

Read More from http://www.3pillarglobal.com/blog/parcelable-vs-java-serialization-android-app-development

Answer:

both parceling and serializing are ways to marshall and unmarshal data. in android this is used to pass non-primitive data types between components and processes. in general, android allows either serializable or parcelable objects, so you can choose your method. the exception to that is with AIDL interfaces. objects must be parcelable to be passed / returned.

serialization uses reflection to automatically marshall and unmarshal data. in most cases implementing the marker interface is enough to make it just work. with parceling, you have to write the code to marshall and unmarshal the data yourself.

and hence, that is why parceling is faster. the object does not need to be reflected to discover the fields. it’s the reflection that makes it slow.

serialization also has in-built versioning … if you try to unmarshal to a different version of the object’s class that was marshalled, the process will fail in a predictable way. with parceling, you can do the same thing but you need to implement it yourself by adding a “version field to your object, and code that checks the version when unmarhsaling.

that being said, i typically use serialization. with simple objects you won’t notice the difference. you can always change to use parceling later in development if you discover performance issues.