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 required 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 not required

 

Let’s move and differentiate 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.; readExternal() and writeExternal()
During Serialization process, all member variables of an object is serialized (even if some of the variables not 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 custom logic for serialization to happen
From above stated points, it is clear that JVM takes the complete control over serialization process Programmer has complete control over serialization process (to write custom logic for the 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 thrownThis is mainly required during readExternal() method; that is 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 custom logic to ignore those variables which is not requiredSo, transient modifier doesn’t play any important role in externaizable
This is the best suit; when whole or complete object required to be serialized to the file storage This is the best suit; when partial object or few of the member variables of an object need to be serialized to the file storage

 

 

 

References:

https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
https://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html
https://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.3

 

Read Also:

 

Happy Coding !!
Happy Learning !!