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 process | Externalizable is a sub-interface of Serializable interface and contains 2 methods viz.;
|
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 |
Serializable interface | Externalizable interface with example |
Related Articles:
- Java – Serialization and De-Serialization Tutorial Index
- Java – Introduction to Serialization and De-Serialization
- Java – Serializable interface
- Java – Transient keyword with Serialization
- Java – Transient keyword with static variable in Serialization
- Java – Transient keyword with final variable in Serialization
- Java – Serializing a variable with transient modifier or keyword
- Java – Order of Serialization and De-Serialization
- Java – Serialization with Aggregation
- Java – Serialization with Inheritance
- Java – Externalization in detail
- Java – Serializable v/s Externalizable
- Java – Importance of SerialVersionUID in Serialization
- Java – Singleton Design pattern with Serialization
- Java – How to construct a singleton class in a multi-threaded environment ?
- Java – Singleton design pattern, restricting Object creation by overriding readResolve() method
- Java – How to stop Serialization ?
- Java – How to serialize and de-serialize ArrayList ?
- Java – Interview question & answers on Serialization and Externalization
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
Happy Coding !!
Happy Learning !!