DO CHECK BONUS INFO AT THE END
What is Serializable ?
To understand serializable, firstly, we need to understand what serializable objects are.
What is Serializable object?
Serializable object means converting an object into bytestream and deserializing means reverting a bytestream into copy of the object.
To create serializable objects, we use an interface called as Serializable which is a part of java.io.Serializable interface and it’s a marker interface (Interface that doesn’t have any method in it, just there to tell the compiler a particular operation to do)
As we can easily save a bytestream of data in a file or send to another Android Component (Activity, Fragment, Services etc)
What is Parcelable ?
Let’s understand Parcelable objects now. Are they same as Serializable objects? We will see…
Same as Serializable, they are also used to convert objects into bytestream but then What is Different? I will answer that shortly. Let’s come back to Parcelable.
Parcelable object is created by using Parcelable interface and overriding its method. Class must have a non-null static field called CREATOR with the type Parcelable.Creator. It means by overriding the methods, we are giving concrete implementation to the compiler, on how to convert objects into bytes. This means we have control over it and also it’s the developer’s responsibility to build the parcel object. So compiler reacts faster over parcelable because of the rules the developer already provided on how to convert.
That is something different than Serializable. That is the reason Parcelable is faster than Serializable. Furthermore, Parcelable is highly optimized for IPC (Inter-Process Communication, used to communicate with Android Components, e.g. Intent, Bundle, Binders etc).
Furtheerrrrrmorrreee, Serialzable uses java reflection mechanism that creates temporary objects in the memory, so it is not memory efficient as compared to Parcelable.
BONUS CONTENT
Why do we need Serializable or Parcelable?
Consider that you can pass reference of an object from one place to another using Intent or Bundle. There might be a scenario which your app is in the background and its process is killed in case of low memory. When you come back to the application, O.S clears the memory stack and creates a new process for the application. Now, the references won’t be there because, after the process kill, the memory stack clears out where all the object references are stored (Stack Memory). IT’S AN ISSUE
That’s where Serializable and Parcelable comes to the picture.
So the O.S instead of saving the references, needs to store the values of those objects and it would only be possible either using Serializable and Parcelable interfaces.
WOLLA THAT”S ALL.
Thank You for Reading.
1 Comment
https://www.waste-ndc.pro/community/profile/tressa79906983/
April 26, 2024You really make it seem so easy with your presentation but I ind this matter to be really something that I thin I would never understand. It seems too complicatsd and extremely broad for me. I'm looking forward for your next post, I will try too get the hang of it!