Serialization, Deserialization, and Externalization in Java
Serialization is the process of converting an object into a byte stream, so it can be easily saved to a file, sent over a network, or stored in a database. Deserialization is the reverse process where the byte stream is converted back into a copy of the original object. Externalization is a more controlled way of serialization, giving the developer full control over what gets serialized and how.
1. Serialization
When an object is serialized, the entire state of the object is stored, including all the fields that are non-transient and non-static.
import java.io.*;
class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "Employee{name='" + name + "', id=" + id + "}";
}
}
public class SerializationExample {
public static void main(String[] args) {
Employee employee = new Employee("John Doe", 12345);
// Serialize the object
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employee.ser"))) {
oos.writeObject(employee);
System.out.println("Serialization complete: " + employee);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This code creates an “Employee” object, serializes it to a file named “employee.ser”.
2. Deserialization
Deserialization is the process of converting the serialized byte stream back into a copy of the original object.
mport java.io.*;
public class DeserializationExample {
public static void main(String[] args) {
Employee employee = null;
// Deserialize the object
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("employee.ser"))) {
employee = (Employee) ois.readObject();
System.out.println("Deserialization complete: " + employee);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
The code above reads the serialized “Employee” object from the “employee.ser” file and converts it back into an “Employee” object.
3. Externalization
Externalization is an alternative to Serialization where you have complete control over the serialization process. You have to explicitly define how the object’s fields are written to and read from the byte stream.
import java.io.*;
class ExternalizableEmployee implements Externalizable {
private String name;
private int id;
public ExternalizableEmployee() {
// Required no-arg constructor for Externalization
}
public ExternalizableEmployee(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeInt(id);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = in.readUTF();
id = in.readInt();
}
@Override
public String toString() {
return "ExternalizableEmployee{name='" + name + "', id=" + id + "}";
}
}
public class ExternalizationExample {
public static void main(String[] args) {
ExternalizableEmployee employee = new ExternalizableEmployee("Jane Doe", 67890);
// Serialize the object
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employee_ext.ser"))) {
oos.writeObject(employee);
System.out.println("Externalization complete: " + employee);
} catch (IOException e) {
e.printStackTrace();
}
// Deserialize the object
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("employee_ext.ser"))) {
ExternalizableEmployee deserializedEmployee = (ExternalizableEmployee) ois.readObject();
System.out.println("Deserialization complete: " + deserializedEmployee);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
In the example above, “writeExternal” method writes the “name” and “id” fields to the stream and the “readExternal” method reads them back. This gives the full control over what gets serialized and how, allowing you to optimize the process or add extra validation logic.
In conclusion, Serialization and Deserialization automatically handles serialization and converts the serialized byte stream back to an object respectively, with minimal coding effort, but you have less control. While Externalization on the other hand provides full control over the serialization process, requiring the implementation of “writeExternal” and “readExternal” methods.