what is difference between Parcelable and Serialization used in android
Asked Answered
M

5

34

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;
    }
}
Mallorymallow answered 18/6, 2012 at 6:32 Comment(3)
Did you go through these - #3323574 and #5551170 ?Demanding
Use capital letters to start off a class name (GetSetClass or Getsetclass).Philbert
Whats the need of serializing of objects in android? may we don't perform the task without serializing the objects?Mallorymallow
C
19

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

Coal answered 18/6, 2012 at 6:41 Comment(3)
@hi arun George ,..why should i use serialization for only non-primitive data type ? and why it is compulsory for non- primitive dat type only?Mallorymallow
Primitive data types can be passed from one activity to another using intent.putExtras(String id, PRIMITIVE_DATA_TYPE) and accessed in the other other activity using the getMethod() equivalent for that type. But such is not the case for user defined data types and thus you have to use either serialization or Parcelable.Coal
ok thanks for answering one more question ..when should we use Bundle for sending data and whats the need to put object in Bundle intent.putExtras(String id, PRIMITIVE_DATA_TYPE) if we r using serialization for sending data..Mallorymallow
U
30

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.

Unloosen answered 4/12, 2013 at 12:57 Comment(2)
I have no idea why this answer hasn't got any upvote until now, it's quite complete and to be honest, one of the bests for me.Jepum
@vsvankhede Well, it seems there is a time and place for everything and currently it's the most upvoted one.Jepum
C
19

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

Coal answered 18/6, 2012 at 6:41 Comment(3)
@hi arun George ,..why should i use serialization for only non-primitive data type ? and why it is compulsory for non- primitive dat type only?Mallorymallow
Primitive data types can be passed from one activity to another using intent.putExtras(String id, PRIMITIVE_DATA_TYPE) and accessed in the other other activity using the getMethod() equivalent for that type. But such is not the case for user defined data types and thus you have to use either serialization or Parcelable.Coal
ok thanks for answering one more question ..when should we use Bundle for sending data and whats the need to put object in Bundle intent.putExtras(String id, PRIMITIVE_DATA_TYPE) if we r using serialization for sending data..Mallorymallow
D
6

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

Dyaus answered 19/6, 2015 at 7:43 Comment(0)
F
3

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

Featherbedding answered 4/12, 2013 at 11:34 Comment(1)
Highlighting the problem with serialization gives this answer my +1Septennial
G
2

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.

Gullible answered 1/8, 2013 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.