Serializable v/s Externalizable

In this article, we will discuss difference between Serialization and Externalization in detail i.e.; Serializable v/s Externalizable

The main difference between these serializing concepts is

  • Serialization helps in serializing complete object whereas with Externalization we can serialize either complete or partial object
  • Performance-wise Externalization is far better than Serialization, as only partial member variables of an object is considered while serializing to file storage
  • And in serialization complete object is compulsorily considered while serializing to file storage, even if some of the member variables aren’t required

Let’s move and differentiate between these two serializing principle in more detail in tabular form;

 

Serializable v/s Externalizable:

Serializable  Externalizable
Serializable is a marker interface which doesn’t contain any methods and JVM provides special ability during serialization process Externalizable is a sub-interface of Serializable interface and contains 2 methods viz.;

  1. readExternal();
  2. writeExternal();
During Serialization process, all member variables of an object is serialized, even if some of the variables aren’t required to be serialized But in Externalization, programmer has to provide serialization logic
That’s why, it is referred as default serialization This is referred as custom serialization, as programmer has to write/code custom logic for serialization to happen
From above stated points, it is clear that JVM takes complete control over serialization process Programmer has complete control over serialization process to write custom logic for required variables to be serialized
Performance-wise, Serializable is relatively low as complete object need to be serialized, even if we require only partial object Performance is high in extenalizable, as programmer design what all required variable need to be serialized
Doesn’t require any public no-argument constructor for serializable Public no-argument constructor is very must in externalizable

Otherwise InvalidClassException is thrown

This is mainly required during readExternal(); method;

i.e.; while restoring object back to heap memory from file storage

For variable that needn’t to be serialized use transient modifier but still its default value is stored into file

Transient modifier play a very important role in serializable

Variable with transient modifier not required; as programmer can write/code custom logic to ignore those variables which is not required

So, transient modifier doesn’t play any important role in externaizable

This is the best suit; when whole/complete object required to be serialized to file storage This is the best suit; when partial object or few of the member variables of an object need to be serialized to file storage

 

Read Also:

 

References:

 

Happy Coding !!
Happy Learning !!

Importance of SerialVersionUID in Serialization
Externalizable interface with example