Difference Serializable vs Externalizable
In Java, both the Serializable vs Externalizable interfaces are used to enable object serialization, but they have key differences in how they handle the serialization process.
Table of Contents
Serializable Interface
The Serializable interface is a marker interface with no methods. When a class implements Serializable, it indicates to the Java Virtual Machine (JVM) that the objects of this class can be serialized. The default serialization process is handled by the JVM, which uses reflection to save and restore the object’s state.
Advantages of Serializable
1. Ease of Use: Requires minimal effort, just implement the interface.
2. Automatic Handling: The JVM handles the serialization process automatically.
Externalizable Interface
The Externalizable interface extends Serializable and adds two methods: writeExternal and readExternal. These methods must be implemented by the class to define the custom serialization and deserialization logic.
Advantages of Externalizable
1. Custom Serialization: Allows fine-grained control over the serialization process.
2. Performance: Can be more efficient if the custom logic is optimized.
Key Differences Serializable vs Externalizable
1. Control
- Serializable: The JVM handles the serialization process.
- Externalizable: The developer has full control over the serialization process.
2. Methods
- Serializable: No methods to implement.
- Externalizable: Must implement writeExternal and readExternal methods.
3. Performance
- Serializable: Can be less efficient due to the default JVM handling.
- Externalizable: Potentially more efficient with optimized custom logic.
- Let’s illustrate the differences with examples of both interfaces.
java
import java.io.Serializable;
public class SerializablePerson implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public SerializablePerson(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "SerializablePerson{name='" + name + "', age=" + age + "}";
}
}
2. Serialization and Deserialization:
java
import java.io.*;
public class SerializableExample {
public static void main(String[] args) {
SerializablePerson person = new SerializablePerson("Alice", 30);
// Serialization
try (FileOutputStream fileOut = new FileOutputStream("person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(person);
System.out.println("Serialized data is saved in person.ser");
} catch (IOException i) {
i.printStackTrace();
}
// Deserialization
try (FileInputStream fileIn = new FileInputStream("person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
SerializablePerson deserializedPerson = (SerializablePerson) in.readObject();
System.out.println("Deserialized Person:");
System.out.println(deserializedPerson);
} catch (IOException | ClassNotFoundException i) {
i.printStackTrace();
}
}
}
Externalizable Example
1. Externalizable Class:
java
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class ExternalizablePerson implements Externalizable {
private String name;
private int age;
// Default constructor is required for Externalizable
public ExternalizablePerson() {}
public ExternalizablePerson(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(age);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = in.readUTF();
age = in.readInt();
}
@Override
public String toString() {
return "ExternalizablePerson{name='" + name + "', age=" + age + "}";
}
}
2. Serialization and Deserialization:
java
import java.io.*;
public class ExternalizableExample {
public static void main(String[] args) {
ExternalizablePerson person = new ExternalizablePerson("Bob", 40);
// Serialization
try (FileOutputStream fileOut = new FileOutputStream("personExternal.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(person);
System.out.println("Serialized data is saved in personExternal.ser");
} catch (IOException i) {
i.printStackTrace();
}
// Deserialization
try (FileInputStream fileIn = new FileInputStream("personExternal.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
ExternalizablePerson deserializedPerson = (ExternalizablePerson) in.readObject();
System.out.println("Deserialized Person:");
System.out.println(deserializedPerson);
} catch (IOException | ClassNotFoundException i) {
i.printStackTrace();
}
}
}
Explanation of Examples
Serializable Example
- The SerializablePerson class implements Serializable with no additional methods.
- The serialization and deserialization processes are handled by the JVM.
Externalizable Example
- The ExternalizablePerson class implements Externalizable and provides implementations for writeExternal and readExternal.
- The custom serialization logic is defined in these methods, allowing Serializable vs Externalizable for precise control over the process.