Parcelable vs Serializable

Authors
Parcelable vs Serializable

Parcelable is an Android-specific interface that does not use reflection, so it is faster. Serializable is a Java marker interface that is easy to implement, but it uses reflection and creates many temporary objects, so it is slower.

In this blog, we will learn about the difference between Parcelable and Serializable. We will also learn how they work internally.

I am Amit Shekhar, 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.

I teach AI and Machine Learning, and Android at Outcome School.

Join Outcome School and get a high-paying tech job:

Let's get started.

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.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

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.

ParcelableSerializable
Android-Specific.Java.
Faster.Slower.
Less Memory Usage.More Memory Usage.

This was all about Parcelable and Serializable.

To go deep into Android serialization, Parcelable vs Serializable, and other Android internals, check out the Android Program by Outcome School.

Frequently Asked Questions

Why can't we pass a plain data class between activities directly?

We cannot simply pass an object like a Developer data class between activities or fragments using intents or bundles. The object must first be serialized. So, we have to implement either Parcelable or Serializable on the class.

Do we have to write all the Parcelable boilerplate by hand?

No. We can use the kotlin-parcelize plugin and annotate the class with @Parcelize. The implementation is then generated automatically, similar to what we would write by hand, so no boilerplate code is needed.

Why is Serializable slower than Parcelable?

Serializable is slower because it uses reflection and creates many temporary objects. This leads to higher memory usage and potential performance issues. Parcelable does not use reflection and creates fewer temporary objects, which reduces garbage collection overhead.

Is Serializable an Android-specific interface?

No. Serializable is a Java marker interface. It has no methods to override, so it is easy to implement. Parcelable, on the other hand, is an Android-specific interface.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.

Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.