Difference Between Serialization and Externalization in Java



Serialization and externalization both are the processes of converting an object to stream byte and storing byte stream in database or memory. The class that implements java.io.Serializable interface can be serialized. 

What is Serialization?

Java provides a mechanism called object serialization where an object can be converted into a byte stream that includes the object's data and details about the object's type.

Example

The following is an example of Serialization in Java:

import java.io.Serializable;

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

public class Main {
    public static void main(String[] args) {
        SerializableExample obj = new SerializableExample();
        obj.name = "Tutorialspoint";
        System.out.println("Object name: " + obj.name);
    }
}

The output of the above Java program is:

Object name: Tutorialspoint

What is Externalization?

Externalization is used for custom serialization based on the requirement in the application. If a class implements an Externalizable interface then, object serialization will be done using the writeExternal() method. Externalization extends java.io.Serializable.

Example

The following is an example of Externalization in Java:

import java.io.*;

class ExternalizableExample implements Externalizable {
    Integer id;

    // Public no-argument constructor is required
    public ExternalizableExample() {
        // Optional initialization can be done here if needed
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeInt(id);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        this.id = in.readInt();
    }
}

public class Main {
    public static void main(String[] args) {
        // Create an instance and assign a value
        ExternalizableExample example = new ExternalizableExample();
        example.id = 123;

        // Serialization: write the object to a file
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.ser"))) {
            out.writeObject(example);
            System.out.println("Serialized successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Deserialization: read the object back from the file
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.ser"))) {
            ExternalizableExample newExample = (ExternalizableExample) in.readObject();
            System.out.println("Deserialized ID: " + newExample.id);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

The output of the above Java program is:

Serialized successfully.
Deserialized ID: 123

Difference between Serialization and Externalization

The following are the differences between Serialization and Externalization:

Sr. No. Key Serialization Externalization
1
Interface
Serialization is a marker interface 
Externalization contains two methods readExternal and writeExternal. 

Implementation logic 
The class which is implementing this interface gives the responsibility to JVM for serializing or persist java object.  JVM use readObject and writeObject for serialization 
Externalization provides implementation logic control to the application by overriding readExternal and writeExternal methods.

Way to ignore variables 
In serialization, JVM ignores transient variables during the serialization and deserialization of a java object 
Programmer can write their own logic to ignore some of the variables during externalization of java object 

Performance 
A serializable interface uses reflection which causes relatively slow performance.
Externalizable gives full control over the implementation approach. 

Object serialization with inheritance 
1. If the superclass is not serializable then the subclass still can be serialized.
2. If a subclass is not serialized but the superclass is automatically serializable 
We can apply this to externalizable as well.
Updated on: 2025-04-15T19:11:58+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements