English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The Difference Between Serialization and Externalization in Java

Serialization and externalization are processes of converting objects into a stream of bytes and storing the byte stream in a database or memory. Classes that implement the java.io.Serializable interface can be serialized. On the other hand, externalization is used for custom serialization according to the requirements of the application. Externalization extends java.io.Serializable. 

Serial NumberKeySerializationExternalization
1
Interface
Serialization is a marker interface 
Externalization includes two methods readExternal and writeExternal. 
2 
Implementation Logic 
Classes implementing this interface delegate the responsibility of serialization or persistence to Java. JVM uses readObject and writeObject for serialization 
Externalization provides application logic control by overriding the readExternal and writeExternal methods.
3 
Method to ignore variables 
In serialization, the JVM ignores transient variables during the serialization and deserialization of Java objects 
Programmers can write their own logic to ignore certain variables during the Java object externalization process 
4 
Performance 
In the Serializable interface, reflection can lead to performance degradation.
Externalization allows complete control over the implementation method. 
5 
Object Serialization with Inheritance 
1.If the superclass is not serializable, the subclass can still be serialized.
2.If the subclass is not serialized but the superclass is automatically serialized 
We can also apply it to externalizable.

Externalizable Example

class ExternalizableExample implements Externalizable {
   Integer id;
   @Override
   public void writeExternal(ObjectOutput out) throws IOException {
      out.writeInt(id);
   {}
   @Override
   public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
      this.id = in.readInt();
   {}
{}

Serializable Example

class SerializableExample implements Serializable {
   private static final long serialVersionUID = 5081877L;
   String name;
{}