Parcelable vs Serializable
- Authors
- Name
- Amit Shekhar
- Published on
I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
Join Outcome School and get high paying tech job: Outcome School
Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
In this blog, we will learn about the difference between Parcelable and Serializable. We will also learn how they work internally.
Let's start with a scenario: Suppose in Android, we need to pass the following Developer
object between activities or fragments using intents or bundles.
data class Developer(val name: String, val age: Int)
We can't simply pass this Developer
object between activities or fragments using intents or bundles.
We have two solutions as follows:
Implement Parcelable:
data class Developer(val name: String, val age: Int) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readInt()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(name)
parcel.writeInt(age)
}
// code removed for brevity
companion object CREATOR : Parcelable.Creator<Developer> {
override fun createFromParcel(parcel: Parcel): Developer {
return Developer(parcel)
}
override fun newArray(size: Int): Array<Developer?> {
return arrayOfNulls(size)
}
}
}
A lot of boilerplate code, right?
To avoid this, we can use the kotlin-parcelize
plugin and annotate the class with @Parcelize
.
@Parcelize
data class Developer(val name: String, val age: Int) : Parcelable
When we annotate a class with @Parcelize
, the implementation is automatically generated, similar to what we wrote above.
No boilerplate code is needed now. They are automatically generated now.
This was the first solution: By implementing Parcelable.
Now, let's discuss the second solution: By implementing Serializable.
Implement Serializable:
data class Developer(val name: String, val age: Int) : Serializable
Both of the above-mentioned solutions work, but they differ in terms of performance. We will discuss this below.
So, in Android development, both Parcelable and Serializable are the interfaces used to serialize objects so they can be passed between activities or fragments using intents or bundles.
Let's understand the difference between Parcelable and Serializable.
Parcelable
Parcelable is an Android-specific interface designed to serialize objects to pass them through Intent and Bundle .
When we implement Parcelable, we either write the implementation manually or use a plugin that generates it for us.
It does NOT use reflection, and it creates fewer temporary objects during the serialization process, which reduces garbage collection overhead.
Two things to note:
- Parcelable does NOT use reflection.
- Parcelable is an Android-specific interface.
So, Parcelable is faster than Serializable.
Serializable
A serializable interface in Java is a marker interface that allows an object to be converted into a stream of bytes (serialized) and then reconstructed back into an object (deserialized).
It is easy to implement since it requires the class to simply implement the Serializable interface, which is a marker interface with no methods to override.
It uses reflection, and it creates many temporary objects, leading to higher memory usage and potential performance issues.
So, Serializable is slower than Parcelable.
We now understand the differences between Parcelable and Serializable.
Let me tabulate the difference between Parcelable and Serializable.
Parcelable | Serializable |
---|---|
Android-Specific. | Java. |
Faster. | Slower. |
Less Memory Usage. | More Memory Usage. |
This was all about Parcelable and Serializable.
Prepare yourself for Android Interview: Android Interview Questions
That's it for now.
Thanks
Amit Shekhar
Co-Founder @ Outcome School
You can connect with me on:
Follow Outcome School on: