Serializable vs Parcelable in Android
Q: Can you explain the differences between Serializable and Parcelable interfaces in Android? Which one would you use for performance-critical applications?
- Android
- Mid level question
Explore all the latest Android interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Android interview for FREE!
Both Serializable and Parcelable are
interfaces in Java that allow you to serialize an object into a stream of
bytes, which can then be passed between different components in an Android app,
such as activities, services, and broadcast receivers. However, there are some
differences between the two interfaces.
The Serializable interface is a standard Java
interface that is used for object serialization. It is simple to implement, as
it requires no additional methods to be added to the class that is being
serialized. The serialization and deserialization processes are performed automatically
by the Java runtime environment. However, Serializable is relatively
slow and can have a negative impact on app performance, especially when used to
serialize large or complex objects.
On the other hand, the Parcelable interface is an
Android-specific interface that is optimized for performance. It requires a bit
more work to implement than Serializable, as you need to implement
several additional methods, including writeToParcel and createFromParcel.
However, the serialization and deserialization processes are faster and more
efficient than with Serializable. This makes Parcelable a better
choice for performance-critical applications, such as apps that handle large
amounts of data or need to perform frequent serialization and deserialization
operations.
To summarize, if performance is a concern, Parcelable
is the preferred choice over Serializable. However, if performance is
not an issue, Serializable is simpler to implement and may be sufficient
for most use cases.


