Java – 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;

1. Serializable v/s Externalizable:

Serializable Externalizable
Serializable is a marker interface which doesn’t contain any methods and JVM provides special ability during serialization processExternalizable 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 serializedBut in Externalization, programmer has to provide serialization logic
That’s why, it is referred as default serializationThis 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 processProgrammer 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 objectPerformance is high in extenalizable, as programmer design what all required variable need to be serialized
Doesn’t require any public no-argument constructor for serializablePublic 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 storageThis is the best suit; when partial object or few of the member variables of an object need to be serialized to file storage
Serializable interfaceExternalizable interface with example

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Importance of SerialVersionUID in Serialization
Java - Externalizable interface with example